如何在Python中的一个图中设置子图

时间:2014-07-25 17:40:17

标签: python matplotlib figure

我只是想以下列方式在我的图中对齐我的子图:

使用此代码:

fig = plt.figure()
ax1 = fig.add_subplot(131)
a = ax1.imshow(im,interpolation='none',origin='lower')
ax2 = fig.add_subplot(132)
b = ax2.imshow(best_fit,origin='lower')
ax3 = fig.add_subplot(133)
c = ax3.imshow(residual,interpolation='none',origin='lower')
ax4 = fig.add_subplot(123)
d = ax3.imshow(correlation_mat,interpolation='none',origin='lower',vmin=-1,vmax=1)

我想创建一个图形,使得绘图a,b和c都在左边的一列中,然后d占据绘图右侧的大部分空间。我知道这应该在

中说明
fig.add_subplot() 

行,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:4)

subplot2grid是一个非常好的功能,适用于更复杂的布局。您可以定义网格并在网格位置向其添加轴,在这种情况下,我使用3列和3行。您可以为覆盖多个网格位置的轴定义行和列。

import matplotlib.pyplot as plt                                                  

fig = plt.figure()                                                               

ax1 = plt.subplot2grid((3,3), (0,0))                                             
ax2 = plt.subplot2grid((3,3), (1,0))                                             
ax3 = plt.subplot2grid((3,3), (2,0))                                             
ax3 = plt.subplot2grid((3,3), (0,1), rowspan=3, colspan=2)                       
fig.tight_layout()                                                               

结果如下所示:plot result