Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting matplotlib colorbar range

Tags:

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?

like image 662
mgilson Avatar asked Mar 07 '13 21:03

mgilson


People also ask

How do you normalize a Colorbar in Python?

norm = normi; #mpl. colors. Normalize(vmin=-80, vmax=20); plt. axis([1, 1000, -400, 400]);

How do I change the color bar in Matplotlib?

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.

How do you normalize Imshow?

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).


2 Answers

Arg. It's always the last thing you try:

quadmesh.set_clim(vmin=0, vmax=15) 

works.

like image 90
mgilson Avatar answered Sep 21 '22 16:09

mgilson


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() 
like image 42
The Red Gator in Virginia Avatar answered Sep 20 '22 16:09

The Red Gator in Virginia