Python - 图形与 matplotlib 的重叠

Python - Overlap of figures with matplotlib

我在 matplolib 中绘制了子图,但我有一些重叠,我无法更正它,如下所示:

我尝试这样做,但是,我不能移动每个地块...另一种方法可能是删除网格的数字 "pixels"(不是 plt.axis(off)!我会喜欢为右图和中间图保留网格)

plt.close()

fig, axes = plt.subplots(nrows=1, ncols=3)

plt.tight_layout(pad=0.1, w_pad=1, h_pad=2.0)
ax1 = plt.subplot(131) # creates first axis
ax1.set_xticks([0,2000,500,1000,1500])
ax1.set_yticks([0,2000,500,1000,1500])
i1 = ax1.imshow(U,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
plt.colorbar(i1,ax=ax1,ticks=[U.min(),(U.min()+U.max())/2., U.max()],fraction=0.046, pad=0.04,format='%.2f')

ax1.set_title("$ \mathrm{Ux_{mes} \/ (pix)}$")
ax2 = plt.subplot(132) # creates second axis
ax2.set_xticks([0,2000,500,1000,1500])
ax2.set_yticks([0,2000,500,1000,1500])
i2=ax2.imshow(UU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax2.set_title("$\mathrm{Ux_{cal} \/ (pix)}$")
plt.colorbar(i2,ax=ax2,fraction=0.046, pad=0.04,ticks=[UU.min(),(UU.min()+UU.max())/2.,UU.max()],format='%.2f')


ax3 = plt.subplot(133) # creates first axis
ax3.set_xticks([0,2000,500,1000,1500])
ax3.set_yticks([0,2000,500,1000,1500])
i3 = ax3.imshow(resU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax3.set_title("$\mathrm{\mid Ux_{mes} - Ux_{cal} \mid \/ (pix)}$ ")
plt.colorbar(i3,ax=ax3,fraction=0.046, pad=0.04,ticks=[resU.min(), (resU.min()+resU.max())/2.,resU.max()],format='%.2f')
#plt.savefig('test.eps', format='eps', dpi=1000,  bbox_inches='tight', pad_inches=0.1)
plt.savefig('test.png', bbox_inches='tight', pad_inches=0.1)

#plt.show()

这将删除中间和右侧图中垂直轴上的刻度标签(我认为这就是你说 'remove the number "pixels"' 时的意思):

ax2.set_yticklabels([])
ax3.set_yticklabels([])

好吧,您可以将颜色条数字四舍五入到小数点后 1 位或 2 位,这样它们就不会与第二个和第三个子图的 y 轴数字重叠。您还可以添加 ax.get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) 控制自动收报机。可以在此处找到更多详细信息 at this website

你可以做到

tick = np.linspace(min(your_variable),max(your_variable),3)
tick = np.round(tick,decimals=1) # rounding off here

plt.tight_layout(pad=0.5, w_pad=2.5, h_pad=2.0)
ax1 = plt.subplot(131) # creates first axis
ax1.set_xticks([0,2000,500,1000,1500])
ax1.set_yticks([0,2000,500,1000,1500])
ax1.get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())    
ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())` 
i1 = ax1.imshow(U,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
plt.colorbar(i1,ax=ax1,ticks=tick)

ax1.set_title("$ \mathrm{Ux_{mes} \/ (pix)}$")
ax2 = plt.subplot(132) # creates second axis
ax2.set_xticks([0,2000,500,1000,1500])
ax2.set_yticks([0,2000,500,1000,1500])
i2=ax2.imshow(UU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax2.set_title("$\mathrm{Ux_{cal} \/ (pix)}$")
plt.colorbar(i2,ax=ax2,ticks=tick)

ax3 = plt.subplot(133) # creates first axis
ax3.set_xticks([0,2000,500,1000,1500])
ax3.set_yticks([0,2000,500,1000,1500])
i3 = ax3.imshow(resU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax3.set_title("$\mathrm{\mid Ux - Ux \mid \/ (pix)}$ ")
plt.colorbar(i3,ax=ax3,ticks=tick)

plt.show()