user3601754
user3601754

Reputation: 3862

Python - Overlap of figures with matplotlib

I have plot subplots in matplolib, but i have some overlaps and i cant correct it as you can see below :

enter image description here

I try with that, but, i cant move each plots...an other way could be to remove the number "pixels" of the grid (not plt.axis(off)! i would like to keep the grid for the right and middle plots)

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()

Upvotes: 1

Views: 2171

Answers (2)

Srivatsan
Srivatsan

Reputation: 9363

Well you can round off your colorbar numbers to 1 or 2 decimal places so that they don't overlap with the y-axis numbers of your second and third subplot. You can also add ax.get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) and ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) to control the ticker. More details can be found here at this website.

You can do

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()

Upvotes: 1

tmdavison
tmdavison

Reputation: 69056

This will remove the tick labels on the vertical axis in the middle and right plots (which I think is what you mean when you say 'remove the number "pixels"'):

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

Upvotes: 1

Related Questions