Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

seaborn - matplotlib: imshow a 2d array with plots of its marginal densities

How can one plot a 2d density with its marginal densities, along the lines of scatterplot-with-marginal-histograms-in-ggplot2 or 2D plot with histograms / marginals, in matplotlib ? In outline,

    # I have --
A = a 2d numpy array >= 0
xdens ~ A.mean(axis=0)
ydens ~ A.mean(axis=1)

    # I want --
pl.imshow( A )
pl.plot( xdens ) narrow, below A
pl.plot( ydens ) narrow, left of A, with the x y axes flipped

Added in 2017: see the lovely example of seaborn.jointplot, also this on SO. (The question was in 2013, before seaborn.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use sharex and sharey with subplots:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

t = np.linspace(0, 31.3, 100)
f = np.linspace(0, 1000, 1000)
a = np.exp(-np.abs(f-200)/200)[:, None] * np.random.rand(t.size)
flim = (f.min(), f.max())
tlim = (t.min(), t.max())

gs = gridspec.GridSpec(2, 2, width_ratios=[1,3], height_ratios=[3,1])
ax = plt.subplot(gs[0,1])
axl = plt.subplot(gs[0,0], sharey=ax)
axb = plt.subplot(gs[1,1], sharex=ax)

ax.imshow(a, origin='lower', extent=tlim+flim, aspect='auto')
plt.xlim(tlim)

axl.plot(a.mean(1), f)
axb.plot(t, a.mean(0))

Which gives you:

subplots


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...