Single colorbar for multiple subplots with different levels

  colorbar, contourf, matplotlib, python, subplot

I am trying to plot 6 different colormaps in a subplot.
The range of values of each subplot is quite different (ex first line [5-7], second line in [2-4], third line [6-8]).

I want them all to be sharing the same color code so that they can be compared between each other. Nonetheless, I also do not want them to share the same levels. Why? Because if I use the same "vmin" and "vmax" for all of them, it will be harder to see the shades as the colorbar will be [2-8] making changes of ~1 less obvious.

Basically, I want one single colormap for all of them, but I do not want each np.contourf to ba spanning the same range of values.

Is my problem clear? Please write below if you would like clarification!

The code and some visuals are attached. subplots

Thank you in advance!

Marco

# FINAL

lvls = 21

x_grid = np.arange(x_focus_1 -200.,x_focus_1   +200.,10.)
y_grid = np.arange(y_focus_1 -200.,y_focus_1   +200.,10.)
z_grid = np.arange(z_focus_1    ,z_focus_1  +10. ,10.)
dimTOT = (len(x_grid)*len(y_grid)*len(z_grid))
Field_rec = Field_rec_horiz

X, Y = np.meshgrid(x_grid,y_grid, indexing='xy')
z_lev = 0 #to len(z_grid)
size_2d = int(dimTOT/len(z_grid))
x_size = len(x_grid)
y_size = len(y_grid)

C1 = Field_rec['Vx'][z_lev*size_2d:(z_lev+1)*size_2d]
C1 = np.array(C1).reshape(x_size,y_size)

C2 = Field_rec['Vy'][z_lev*size_2d:(z_lev+1)*size_2d]
C2 = np.array(C2).reshape(x_size,y_size)

C3 = Field_rec['ws'][z_lev*size_2d:(z_lev+1)*size_2d]
C3 = np.array(C3).reshape(x_size,y_size)



fig, axs = plt.subplots(3,3,figsize= (20,15))
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.25)


axs[0,0].contourf(X, Y, C1, levels=lvls)
axs[0,0].grid(True)
axs[0,0].set( ylabel='u component nn y [m]')
axs[0,0].set( xlabel=' x [m]')
axs[0,0].set_title('[x,y] plane @ z = 52.59m ')


S = axs[1,0].contourf(X, Y, C2, levels=lvls)
axs[1,0].grid(True)
axs[1,0].set( ylabel='v component nn y [m]')
axs[1,0].set( xlabel=' x [m]')



axs[2,0].contourf(X, Y, C3, levels=lvls)
axs[2,0].grid(True)
axs[2,0].set( ylabel=' WS = $sqrt{u^2 + v^2}$ nn y [m]')
axs[2,0].set( xlabel=' x [m]')

# zx

y_grid = np.arange(y_focus_1      ,y_focus_1   +10.,10.)
x_grid = np.arange(x_focus_1 -200.,x_focus_1  +200.,10.)
z_grid = np.arange(z_focus_1  -40.,z_focus_1   +40.,2.)
dimTOT = (len(x_grid)*len(y_grid)*len(z_grid))


X, Z = np.meshgrid(x_grid,z_grid, indexing='xy')
size_2d = int(dimTOT/len(y_grid))
x_size = len(x_grid)
z_size = len(z_grid)

C1 = Field_rec_vert_x['Vx'][z_lev*size_2d:(z_lev+1)*size_2d]
C1 = np.array(C1).reshape(x_size,y_size)

C2 = Field_rec_vert_x['Vy'][z_lev*size_2d:(z_lev+1)*size_2d]
C2 = np.array(C2).reshape(x_size,y_size)

C3 = Field_rec_vert_x['ws'][z_lev*size_2d:(z_lev+1)*size_2d]
C3 = np.array(C3).reshape(x_size,y_size)


axs[0,1].contourf(X, Z, C1, levels=lvls)
axs[0,1].grid(True)
axs[0,1].set( ylabel=' z [m]')
axs[0,1].set( xlabel=' x [m]')
axs[0,1].set_title('[x,z] plane @ y = -11m ')

axs[1,1].contourf(X, Z, C2, levels=lvls)
axs[1,1].grid(True)
axs[1,1].set( ylabel=' z [m]')
axs[1,1].set( xlabel=' x [m]')

axs[2,1].contourf(X, Z, C3, levels=lvls)
axs[2,1].grid(True)
axs[2,1].set( ylabel=' z [m]')
axs[2,1].set( xlabel=' x [m]')


# yz 

x_grid = np.arange(x_focus_1      ,x_focus_1   +10.,10.)
y_grid = np.arange(y_focus_1 -200.,y_focus_1  +200.,10.)
z_grid = np.arange(z_focus_1  -40.,z_focus_1   +40.,2.)
dimTOT = (len(x_grid)*len(y_grid)*len(z_grid))

X, Z = np.meshgrid(y_grid,z_grid, indexing='xy')
size_2d = int(dimTOT/len(x_grid))
x_size = len(y_grid)
z_size = len(z_grid)

C1 = Field_rec_vert_y['Vx'][z_lev*size_2d:(z_lev+1)*size_2d]
C1 = np.array(C1).reshape(x_size,y_size)

C2 = Field_rec_vert_y['Vy'][z_lev*size_2d:(z_lev+1)*size_2d]
C2 = np.array(C2).reshape(x_size,y_size)

C3 = Field_rec_vert_y['ws'][z_lev*size_2d:(z_lev+1)*size_2d]
C3 = np.array(C3).reshape(x_size,y_size)


axs[0,2].contourf(X, Z, C1, levels=lvls)
axs[0,2].grid(True)
axs[0,2].set( ylabel=' z [m]')
axs[0,2].set( xlabel=' y [m]')
axs[0,2].set_title('[y,z] plane @ x = 292m ')

axs[1,2].contourf(X, Z, C2, levels=lvls)
axs[1,2].grid(True)
axs[1,2].set( ylabel=' z [m]')
axs[1,2].set( xlabel=' y [m]')

axs[2,2].contourf(X, Z, C3, levels=lvls)
axs[2,2].grid(True)
axs[2,2].set( ylabel=' z [m]')
axs[2,2].set( xlabel=' y [m]')


plt.colorbar()




plt.show()

Source: Python Questions

LEAVE A COMMENT