matplotlib:使用边界密度图显示二维数组 [英] matplotlib: imshow a 2d array with plots of its marginal densities

查看:165
本文介绍了matplotlib:使用边界密度图显示二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人如何绘制其边缘密度的二维密度, 遵循 scatterplot-with-marginal-histograms-in-ggplot2 或者 带有直方图/边际的二维图 , 在matplotlib中? 在轮廓上,

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


2017年新增:请参见 seaborn.jointplot 还有那里的好例子 还在SO上 . (问题是在2013年,当时还没出生.)


Added in 2017: see seaborn.jointplot and the good examples there, also this on SO. (The question was in 2013, before seaborn.)

推荐答案

您可以将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))

哪个给您:

这篇关于matplotlib:使用边界密度图显示二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆