2017-04-11 48 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

相關問題