我必须绘制一个包含11个子点的图形,如下所示。但由于它是一个奇数,我不知道如何处理子图(4,3,12)去除它...并将最后2个图放在中心 此外,我想增加子图大小,因为空间太重要了。代码如下。
代码是:
plt.close()
fig, axes = plt.subplots(nrows=4, ncols=3)
plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(431) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8)
i1 = ax1.imshow(IIIm,cmap='hot',extent=(0,2000,0,2000),vmin=-0.2,vmax=-0.1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-0.2,-0.15,-0.1],fraction=0.046, pad=0.04,format='%.3f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$n = -3$", y=1.05, fontsize=12)
ax2 = plt.subplot(432) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(IIm,cmap='hot',extent=(0,2000,0,2000),vmin=-0.1,vmax=0.1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = -2$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,ticks=[-0.1,0.0,0.1],fraction=0.046, pad=0.04,format='%.3f')
cb2.ax.tick_params(labelsize=8)
ax3 = plt.subplot(433) # creates first axis
ax3.set_xticks([])
ax3.set_yticks([])
i3 = ax3.imshow(Im,cmap='hot',extent=(0,2000,0,2000),vmin=-1,vmax=-0.2)
i33 = ax3.plot((0,600),(1000,1000),'k-',linewidth=3)
ax3.set_title("$n = -1$ ", y=1.05, fontsize=12)
cb3=plt.colorbar(i3,ax=ax3,ticks=[-1,-0.6,-0.2],fraction=0.046, pad=0.04,format='%.3f')
ax3.set_xticklabels([])
ax3.set_yticklabels([])
cb3.ax.tick_params(labelsize=8)
#plt.gcf().tight_layout()
#plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(434) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8)
i1 = ax1.imshow(ZV_0_modeI,extent=(0,2000,0,2000),cmap=plt.cm.hot,origin="lower", vmin=-1, vmax=1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-1,0, 1],fraction=0.046, pad=0.04,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$ n = 0$", y=1.05, fontsize=12)
ax2 = plt.subplot(435) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(I,cmap='hot',extent=(0,2000,0,2000), vmin=-1, vmax=1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = 1$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,fraction=0.046, pad=0.04,ticks=[-1,0,1],format='%.2f')
cb2.ax.tick_params(labelsize=8)
ax3 = plt.subplot(436) # creates first axis
ax3.set_xticks([])
ax3.set_yticks([])
i3 = ax3.imshow(II,cmap='hot',extent=(0,2000,0,2000),vmin=-1,vmax=1)
i33 = ax3.plot((0,600),(1000,1000),'k-',linewidth=3)
ax3.set_title("$n = 2$ ", y=1.05, fontsize=12)
cb3=plt.colorbar(i3,ax=ax3,fraction=0.046, pad=0.04,ticks=[-1.,0,1.],format='%.2f')
ax3.set_xticklabels([])
ax3.set_yticklabels([])
cb3.ax.tick_params(labelsize=8)
plt.gcf().tight_layout()
plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(437) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8)
i1 = ax1.imshow(III,cmap=plt.cm.hot,origin="lower",extent=(0,2000,0,2000),vmin=-1, vmax=1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-1,0, 1],fraction=0.046, pad=0.04,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$ n = 3$", y=1.05, fontsize=12)
ax2 = plt.subplot(438) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(IV,cmap='hot',extent=(0,2000,0,2000), vmin=-1, vmax=1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = 4$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,fraction=0.046, pad=0.04,ticks=[-1,0,1],format='%.2f')
cb2.ax.tick_params(labelsize=8)
ax3 = plt.subplot(439) # creates first axis
ax3.set_xticks([])
ax3.set_yticks([])
i3 = ax3.imshow(V,cmap='hot',extent=(0,2000,0,2000),vmin=-1,vmax=1)
i33 = ax3.plot((0,600),(1000,1000),'k-',linewidth=3)
ax3.set_title("$n = 5$ ", y=1.05, fontsize=12)
cb3=plt.colorbar(i3,ax=ax3,fraction=0.046, pad=0.04,ticks=[-1.,0,1.],format='%.2f')
ax3.set_xticklabels([])
ax3.set_yticklabels([])
cb3.ax.tick_params(labelsize=8)
plt.gcf().tight_layout()
plt.tight_layout(pad=0.05, w_pad=0.001, h_pad=2.0)
ax1 = plt.subplot(4,3,10) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8)
i1 = ax1.imshow(VI,cmap=plt.cm.hot,origin="lower",extent=(0,2000,0,2000),vmin=-1, vmax=1)
i11 = ax1.plot((0,600),(1000,1000),'k-',linewidth=3)
cb1=plt.colorbar(i1,ax=ax1,ticks=[-1,0, 1],fraction=0.046, pad=0.04,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$ n = 6$", y=1.05, fontsize=12)
ax2 = plt.subplot(4,3,11) # creates second axis
ax2.set_xticks([0])
ax2.set_yticks([])
i2=ax2.imshow(VII,cmap='hot',extent=(0,2000,0,2000), vmin=-1, vmax=1)
i22 = ax2.plot((0,600),(1000,1000),'k-',linewidth=3)
ax2.set_title("$n = 7$", y=1.05, fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,fraction=0.046, pad=0.04,ticks=[-1,0,1],format='%.2f')
cb2.ax.tick_params(labelsize=8)
plt.savefig('filtre.png', dpi=250,bbox_inches='tight', pad_inches=0.1)
plt.show()
答案 0 :(得分:4)
实现所需要的一种方法是使用matplotlibs subplot2grid功能。使用此选项可以设置网格的总大小(在您的情况下为4,3),并选择仅绘制此网格中某些子图中的数据。以下是一个简化示例:
import matplotlib.pyplot as plt
x = [1,2]
y = [3,4]
ax1 = plt.subplot2grid((4, 3), (0, 0))
ax2 = plt.subplot2grid((4, 3), (0, 1))
ax3 = plt.subplot2grid((4, 3), (0, 2))
ax4 = plt.subplot2grid((4, 3), (1, 0))
ax5 = plt.subplot2grid((4, 3), (1, 1))
ax6 = plt.subplot2grid((4, 3), (1, 2))
ax7 = plt.subplot2grid((4, 3), (2, 0))
ax8 = plt.subplot2grid((4, 3), (2, 1))
ax9 = plt.subplot2grid((4, 3), (2, 2))
ax10 = plt.subplot2grid((4, 3), (3, 0))
ax11 = plt.subplot2grid((4, 3), (3, 1))
plt.subplots_adjust(wspace = 0.3, hspace = 0.3) #make the figure look better
ax1.plot(x,y)
ax2.plot(x,y)
ax3.plot(x,y)
ax4.plot(x,y)
ax5.plot(x,y)
ax6.plot(x,y)
ax7.plot(x,y)
ax8.plot(x,y)
ax9.plot(x,y)
ax10.plot(x,y)
ax11.plot(x,y)
plt.show()
这产生了数字:
答案 1 :(得分:0)