Python - add colored bars to multiple plots for each plot

I have several graphs on a figure. I would like to add colorbars for each result. These colored bars need to have the same height of my results, and I would like to limit the numbers displayed in the colored bar to 3 values ​​(to have readable numbers).enter image description here

My code:

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

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.imshow(U,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
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])
ax2.imshow(UU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax2.set_title("$\mathrm{Ux_{cal} \/ (pix)}$")
ax3 = plt.subplot(133) # creates first axis
ax3.set_xticks([0,2000,500,1000,1500])
ax3.set_yticks([0,2000,500,1000,1500])
ax3.imshow(resU,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
ax3.set_title("$\mathrm{\mid Ux - Ux \mid \/ (pix)}$ ")
plt.show()

      

I'm trying to add: "fig.colorbar (U, axes = ax1, fraction = 0.046, pad = 0.04)" but it doesn't work ...

+3


source to share


2 answers


Following @plonser's answer

tick = np.linspace(min(your_variable),max(your_variable),3)

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

      



When you specify, you are doing it with ax=ax1

, not axes=ax1

. Also, to limit the number of items in colorbar

, you can do this using the option ticks

.

+1


source


You can add a color bar with plt.colorbar

. This requires an image as an argument. To appear on the correct axes, it also needs the axle to which it is attached.

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])
i1 = ax1.imshow(U,cmap='hot',extent=(X.min(),2000,Y.min(),2000))
plt.colorbar(i1,ax=ax1,ticks=np.linspace(np.amin(U),np.amax(U),3))

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=np.linspace(np.amin(UU),np.amax(UU),3))

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=np.linspace(np.amin(resU),np.amax(resU),3))

plt.show()

      

Update

If the height of the colored panels is too big, you can solve it with



plt.colorbar(...,fraction=0.046, pad=0.04) 

      

as stated here

In case the graphs overlap, try

plt.gcf().tight_layout()

      

+2


source







All Articles