2017-04-11 42 views
0

我想创建一个如下图所示的图。图中有两个独特的地块。 img1使用plt.imshow()生成,而img2使用plt.plot()生成。我用于生成每个地块的代码被提供如下Matplotlib subplot:imshow + plot

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

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

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

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

Ideal output with annotation

回答

1

您可以使用mpl_toolkits.axes_grid1make_axes_locatable功能来创建沿着中央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() 

enter image description here

+0

真棒!这是我需要的!谢谢! – EFL

相关问题