python - Setting colorbar to show values outside of data range in matplotlib -
i trying create figure in colorbar extend beyond data range (go higher max value of data). ultimate purpose need plot series of images (as time progresses) of model output, , each hour stored in separate file. colorbar figures same, can joined animation.
here sample script:
import matplotlib.pyplot plt import numpy np x = np.arange(0, 360, 1.5) y = np.arange(-90, 90, 1.5) lon, lat = np.meshgrid(x, y) noise = np.random.random(lon.shape) # values in range [0, 1) fig = plt.figure() ax = fig.add_subplot(111) plt.hold(true) plt.contourf(lon, lat, noise) plt.colorbar()
this produces following figure:
i've been trying set limits of colorbar values outside data range (for example, -1. 2.) using 2 methods i've found online:
setting vmin=-1 , vmax=2 inside plotting line:
fig = plt.figure() ax = fig.add_subplot(111) plt.hold(true) plt.contourf(lon, lat, noise, vmin=-1., vmax=2.) plt.colorbar()
this seems change colors displayed, first color in colormap correspond -1 , last 1 2, not extend colorbar show values (left figure in link below).
the other 1 try , enforce ticks in colorbar extend range:
fig = plt.figure() ax = fig.add_subplot(111) plt.hold(true) plt.contourf(lon, lat, noise) plt.colorbar(ticks=np.arange(-1,2.1, .2))
this results in tick position defined, range in there's data, i.e., colorbar still doesn't extend -1 2 (middle figure in link below).
does know how want? right figure @ link: http://orca.rsmas.miami.edu/~ajdas1/sof/n.html
for 2d plotting function (such imshow
, pcolor
, etc.) setting vmin
, vmax
job. however, contourf
(and contour
) take levels
@ ask draw contours account when mapping colors:
if don't specify levels
argument, function automatically generates 10 equally spaced levels minimal maximal value of data. achieve want (consistency on varying input data) have specify levels explicitly:
import matplotlib.pyplot plt import numpy np # generate data x = np.arange(0, 360, 1.5) y = np.arange(-90, 90, 1.5) lon, lat = np.meshgrid(x, y) noise = np.random.random(lon.shape) # specify levels vmim vmax levels = np.arange(-1, 2.1, 0.2) # plot fig = plt.figure() ax = fig.add_subplot(111) plt.contourf(lon, lat, noise, levels=levels) plt.colorbar(ticks=levels) plt.show()
result:
Comments
Post a Comment