python – Matplotlib奇数子图

前端之家收集整理的这篇文章主要介绍了python – Matplotlib奇数子图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我必须绘制一个有11个子点的图,如下所示.但由于这是一个奇数,我不知道如何处理子图(4,3,12)去除它…并将最后2个图放在中心
此外,我想增加子图大小,因为空间太重要了.代码如下.

enter image description here

代码是:

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,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,vmin=-0.1,vmax=0.1)
i22 = ax2.plot((0,linewidth=3)
ax2.set_title("$n = -2$",fontsize=12)
ax2.set_xticklabels([])
ax2.set_yticklabels([])
cb2=plt.colorbar(i2,ax=ax2,ticks=[-0.1,0.0,0.1],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,vmin=-1,vmax=-0.2)
i33 = ax3.plot((0,linewidth=3)
ax3.set_title("$n = -1$",fontsize=12)
cb3=plt.colorbar(i3,ax=ax3,ticks=[-1,-0.6,-0.2],format='%.3f')
ax3.set_xticklabels([])
ax3.set_yticklabels([])
cb3.ax.tick_params(labelsize=8)
#plt.gcf().tight_layout()





#plt.tight_layout(pad=0.05,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,cmap=plt.cm.hot,origin="lower",vmax=1)
i11 = ax1.plot((0,1],format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$n = 0$",fontsize=12)


ax2 = plt.subplot(435) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(I,vmax=1)
i22 = ax2.plot((0,linewidth=3)
ax2.set_title("$n = 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,vmax=1)
i33 = ax3.plot((0,linewidth=3)
ax3.set_title("$n = 2$",ticks=[-1.,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,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,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$n = 3$",fontsize=12)

ax2 = plt.subplot(438) # creates second axis
ax2.set_xticks([])
ax2.set_yticks([])
i2=ax2.imshow(IV,linewidth=3)
ax2.set_title("$n = 4$",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,linewidth=3)
ax3.set_title("$n = 5$",h_pad=2.0)
ax1 = plt.subplot(4,10) # creates first axis
ax1.set_xticks([])
ax1.set_yticks([])
ax1.tick_params(labelsize=8) 
i1 = ax1.imshow(VI,format='%.2f')
cb1.ax.tick_params(labelsize=8)
ax1.set_title("$n = 6$",fontsize=12)

ax2 = plt.subplot(4,11) # creates second axis
ax2.set_xticks([0])
ax2.set_yticks([])
i2=ax2.imshow(VII,linewidth=3)
ax2.set_title("$n = 7$",format='%.2f')
cb2.ax.tick_params(labelsize=8)


plt.savefig('filtre.png',dpi=250,bBox_inches='tight',pad_inches=0.1)

plt.show()
最佳答案
@H_404_17@实现所需要的一种方法是使用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,1))
ax3 = plt.subplot2grid((4,2))
ax4 = plt.subplot2grid((4,(1,0))
ax5 = plt.subplot2grid((4,1))
ax6 = plt.subplot2grid((4,2))
ax7 = plt.subplot2grid((4,(2,0))
ax8 = plt.subplot2grid((4,1))
ax9 = plt.subplot2grid((4,2))
ax10 = plt.subplot2grid((4,(3,0))
ax11 = plt.subplot2grid((4,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()

这产生了这个数字:

enter image description here

原文链接:https://www.f2er.com/python/438630.html

猜你在找的Python相关文章