Matplotlib 奇数子图
Posted
技术标签:
【中文标题】Matplotlib 奇数子图【英文标题】:Matplotlib odd subplots 【发布时间】:2017-02-26 03:33:22 【问题描述】:我必须绘制一个包含 11 个子罐的图形,如下所示。但因为它是一个奇数,我不知道如何处理子图(4,3,12)来删除它......并将最后两个图放在中心 此外,我想增加子图的大小,因为空间太重要了。代码如下。
代码是:
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()
【问题讨论】:
使用fig, axes = plt.subplots(nrows=4, ncols=3)
,您已经创建了该轴实例,因此您需要手动隐藏它,也许这会有所帮助:codeyarns.com/2015/06/29/how-to-hide-axis-of-plot-in-matplotlib 编辑:这:***.com/questions/18603959/…
为什么不使用它们就创建所有axes
?一个简单的fig=pl.figure()
,其余代码不变,应该可以解决这个问题。
【参考方案1】:
实现您所需的一种方法是使用 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()
这会产生图形:
【讨论】:
非常感谢您的贡献!如果你想把最后两个居中? 我不确定是否将底部两个居中,但您可以通过将colspan = 2
添加到 ax11
定义(如 ax11 = plt.subplot2grid((4, 3), (3, 1),colspan=2)
)来增加最后一个子图的大小。这将扩大图形以使用剩余空间。
对“colspan”的结论不太好,但非常感谢您的帮助:)
到 center 底行,您可以使用subplot2grid((4,6),....
创建 6 个 columns 并对所有图使用 colspan=2
并使用 0, 2,4 作为前三行的列索引,1,3 作为底行的列索引。【参考方案2】:
只是不要绘制任何东西,而是关闭轴
ax = plt.subplot(4,3,12)
ax.axis('off')
如有必要,您可以像这样使用循环
images = [rgb, depth, depth, rgb, rgb]
fig, axes = plt.subplots(rows, columns)
for index, axis in enumerate(axes.reshape(-1)):
if index < no_of_images:
axis.imshow(images[index])
axis.axis('off')
【讨论】:
以上是关于Matplotlib 奇数子图的主要内容,如果未能解决你的问题,请参考以下文章