Python Matplotlib子批次:imshow+;情节

Python Matplotlib子批次:imshow+;情节,python,matplotlib,plot,Python,Matplotlib,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))。理想情

我想创建一个类似下图的绘图。图中有两个独特的绘图。
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
)没有显示分布图


您可以使用
mpl\u工具箱的
使轴可定位功能。轴网格1
沿中央
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()

您可以使用
mpl\u工具箱的
使轴可定位功能。轴网格1
沿中央
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()

太棒了!这就是我需要的!谢谢令人惊叹的!这就是我需要的!谢谢