python - How do I set limits on ticks, colors, and labels for colorbar contourf matplotlib -
i've spatial field of values i'm outputting on regular interval on course of day. i'm plotting contourf
, following on course of day's data:
- limit colors on colobar values represent min , max of day's data
- hold color bar static on 24 hourly plots @ max , min of days data
- hold labels static on course of 24 hours
for example:
data = np.random.uniform(0, 5, size=(24,30,30)) data[3,:,:]=np.random.uniform(1,3,size=(30,30)) # example of bad plot fgsize=(12,4) numrecs = np.size(data,axis=0) cbar_min = np.min(data) cbar_max = np.max(data) cbarlabels = np.linspace(np.floor(cbar_min), np.ceil(cbar_max), num=5, endpoint=true) tt in range(0, numrecs): plt.figure(figsize=fgsize, dpi=80) plt.title('this title') plt.contourf(data[tt, :, :], 35, vmin=cbar_min, vmax=cbar_max, cmap='coolwarm') cbar =plt.colorbar() cbar.set_ticks(cbarlabels) cbar.set_ticklabels(cbarlabels) cbar.set_label('my data has units') plt.show() plt.close()
here , example of bad plot. colors seem limited, color bar changes color/label limits. how fix this?
here example of good plot.
it turns out contourf bit tricky in setting levels colormap, see this answer. can proper limits , colours normalising contours, follows:
import numpy np import matplotlib.pyplot plt data = np.random.uniform(0, 5, size=(24,30,30)) data[3,:,:]=np.random.uniform(1,3,size=(30,30)) # example of bad plot fgsize=(12,4) numrecs = np.size(data,axis=0) cbar_min = np.min(data) cbar_max = np.max(data) cbarlabels = np.linspace(np.floor(cbar_min), np.ceil(cbar_max), num=5, endpoint=true) # set normalisation 35 levels (as in example) import matplotlib.colors mc levels = np.linspace(np.floor(cbar_min), np.ceil(cbar_max), 35) # draw 35 levels norm = mc.boundarynorm(levels, 256) tt in range(0, numrecs): print cbar_min, cbar_max plt.figure(figsize=fgsize, dpi=80) plt.title('this title') # draw levels, proper normalisation, here: plt.contourf(data[tt, :, :], levels, vmin=cbar_min, vmax=cbar_max, cmap='coolwarm', levels=levels, norm=norm) cbar = plt.colorbar() cbar.set_ticks(cbarlabels) cbar.set_ticklabels(cbarlabels) cbar.set_label('my data has units') plt.show()
Comments
Post a Comment