matplotlib:使用边缘密度的阴谋显示一个2d数组(matplotlib: imshow a 2d array with plots of its marginal densities)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
matplotlib:使用边缘密度的阴谋显示一个2d数组(matplotlib: imshow a 2d array with plots of its marginal densities)

在matplotlib中,如何绘制一个具有边缘密度的二维密度,沿着散点图 - 边缘 - 直方图 - 在ggplot2或带有直方图/边缘的二维图 ? 大致上,

# 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年,在seaborn之前。)

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 seaborn.jointplot and the good examples there, also this on SO. (The question was in 2013, before seaborn.)

最满意答案

您可以使用sharex和sharey :

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

它给你:

次要情节

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

更多推荐

本文发布于:2023-04-12 20:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/1fa70e7fe579eeb4ef03bce9434bcf37.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   密度   阴谋   边缘   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!