Home > Enterprise >  matplotlib colorbar limits for contourf
matplotlib colorbar limits for contourf

Time:01-10

How do you set hard limits on colorbar used with contourf? The code below works as expected with colorbar limits set to [-3, 3] when using plot_surface, but with contourf the limits are not at the tips of the colorbar. I need this to create a gif from multiple images with a constant colorbar.

import matplotlib.pyplot as plt 
import numpy as np

fig = plt.figure()

ax = fig.gca(projection='3d')
CHI = np.linspace(-45, 45, 35);
M = np.linspace(0, 1, 35) 
CHI, M = np.meshgrid(CHI, M)
R = 10*2*M*np.sin(  2 * np.deg2rad(CHI) )

cont = ax.contourf(CHI, M, R)
#cont = ax.plot_surface(CHI, M, R)
cont.set_clim(vmin=-3, vmax=3)

ax.set_xlim(-45,45)
cbar = plt.colorbar(cont, ticks=[-3,-2,-1,0,1,2,3])
plt.show()

enter image description here enter image description here

CodePudding user response:

You could pass levels parameter to enter image description here

  •  Tags:  
  • Related