繁体   English   中英

Matplotlib子图:imshow +图

[英]Matplotlib subplot: imshow + plot

我想创建一个如下图所示的图。 图中有两个独特的图。 img1是使用plt.imshow()生成的,而img2是使用plt.plot()生成的。 下面提供了我用于生成每个图的代码

plt.clf()
plt.imshow(my_matrix)
plt.savefig("mymatrix.png")

plt.clf()
plt.plot(x,y,'o-')
plt.savefig("myplot.png")

img1使用的矩阵是64x64 img2的x轴的范围相同( x=range(64) )。 理想情况下,两个img2的x轴与img1的轴对齐。

重要的是要注意,最终图像让人联想到seaborn的jointplot() ,但是下图中的边际子图( img2 )没有显示分布图。

带注释的理想输出

您可以使用make_axes_locatable的功能mpl_toolkits.axes_grid1打造沿中心的双向共享轴imshow情节。

这是一个例子:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np; np.random.seed(0)

Z = np.random.poisson(lam=6, size=(64,64))
x = np.mean(Z, axis=0)
y = np.mean(Z, axis=1)

fig, ax = plt.subplots()
ax.imshow(Z)

# create new axes on the right and on the top of the current axes.
divider = make_axes_locatable(ax)
axtop = divider.append_axes("top", size=1.2, pad=0.3, sharex=ax)
axright = divider.append_axes("right", size=1.2, pad=0.4, sharey=ax)
#plot to the new axes
axtop.plot(np.arange(len(x)), x, marker="o", ms=1, mfc="k", mec="k")
axright.plot(y, np.arange(len(y)), marker="o", ms=1, mfc="k", mec="k")
#adjust margins
axright.margins(y=0)
axtop.margins(x=0)
plt.tight_layout()
plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM