5

matplotlibの scatterplot-with-marginal-histograms-in-ggplot2 または 2D プロット with histograms / marginalsの線に沿って、その限界密度で 2D 密度をプロットするにはどうすればよい ですか? 概要では、

    # 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

2017 年に追加: seaborn.jointplotの素敵な例を参照してください。これもSO にあります。(質問は、シーボーンの前の2013年にありました。)

4

1 に答える 1

16

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))

これにより、次のことが得られます。

サブプロット

于 2013-12-11T19:04:48.543 に答える