I would like to set the matplotlib colorbar range. Here's what I have so far:
import numpy as np import matplotlib.pyplot as plt x = np.arange(20) y = np.arange(20) data = x[:-1,None]+y[None,:-1] fig = plt.gcf() ax = fig.add_subplot(111) X,Y = np.meshgrid(x,y) quadmesh = ax.pcolormesh(X,Y,data) plt.colorbar(quadmesh) #RuntimeError: You must first define an image, eg with imshow #plt.clim(vmin=0,vmax=15) #AttributeError: 'AxesSubplot' object has no attribute 'clim' #ax.clim(vmin=0,vmax=15) #AttributeError: 'AxesSubplot' object has no attribute 'set_clim' #ax.set_clim(vmin=0,vmax=15) plt.show()
How do I set the colorbar limits here?
norm = normi; #mpl. colors. Normalize(vmin=-80, vmax=20); plt. axis([1, 1000, -400, 400]);
You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.
Just specify vmin=0, vmax=1 . By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).
Arg. It's always the last thing you try:
quadmesh.set_clim(vmin=0, vmax=15)
works.
Matplotlib 1.3.1 - It looks like the colorbar ticks are only drawn when the colorbar is instanced. Changing the colorbar limits (set_clim) does not cause the ticks to be re-drawn.
The solution I found was to re-instance the colorbar in the same axes entry as the original colorbar. In this case, axes[1] was the original colorbar. Added a new instance of the colorbar with this designated with the cax= (child axes) kwarg.
# Reset the Z-axis limits print "resetting Z-axis plot limits", self.zmin, self.zmax self.cbar = self.fig.colorbar(CS1, cax=self.fig.axes[1]) # added self.cbar.set_clim(self.zmin, self.zmax) self.cbar.draw_all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With