matplotlib:显示一个带有边缘密度图的二维数组

den*_*nis 4 matplotlib seaborn

在matplotlib中,如何绘制具有边际密度的二维密度,沿着 散点图 - 边缘直方图 - 在ggplot22D直方图中绘制直方图/边缘图?概括地说,

    # 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
Run Code Online (Sandbox Code Playgroud)


在2017年添加:请参阅seaborn.jointplot 和那里的好例子,也是在SO上.(问题发生在2013年,在seaborn之前.)

ask*_*han 14

您可以使用sharexsharey子图:

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))
Run Code Online (Sandbox Code Playgroud)

哪个给你:

次要情节