python matplotlib colorbar setting tick formator/locator changes tick labels


Question

users, I want to customize the ticks on a colorbar. However, I found the following strange behavior. I try to change the tick formator to the default formator (I thought this should change nothing at all) but I end up with different labels. Does anybody know what I am doing wrong? Or is this a bug?

I use matplotlib from git (v1.0.1-961-gb516ae0 , git describe). The following code produces the two plots:

#http://matplotlib.sourceforge.net/examples/pylab_examples/griddata_demo.html
from numpy.random import uniform, seed
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
# make up data.
seed(0)
npts = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)
# define grid
xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,200)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')

##### FIRST PLOT
plt.figure()
CS  = plt.contour(xi,yi,zi,25,cmap=plt.cm.jet)
bar = plt.colorbar() # draw colorbar
# plot data points.
#plt.scatter(x,y,marker='o',c='b',s=5,zorder=10)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata test (%d points)' % npts)
plt.show()

##### SECOND PLOT
plt.figure()
CS  = plt.contour(xi,yi,zi,25,cmap=plt.cm.jet)
bar = plt.colorbar() # draw colorbar
bar.ax.yaxis.set_minor_locator(matplotlib.ticker.AutoLocator())
bar.ax.yaxis.set_major_locator(matplotlib.ticker.AutoLocator())
# plot data points.
#plt.scatter(x,y,marker='o',c='b',s=5,zorder=10)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata test (%d points)' % npts)
plt.show()
1
6
6/18/2014 3:19:25 PM

I just found the solution. One has to call

bar.update_ticks()

after the formators/locators are changed, see

http://matplotlib.sourceforge.net/api/colorbar_api.html

Then everything works well.

Update:

Here is also the code which changes the Formator/Locator. It is based on the internal structure of the colorbar-code, so maybe someone else has a better solution:

#http://matplotlib.sourceforge.net/examples/pylab_examples/griddata_demo.html
from numpy.random import uniform, seed
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
# make up data.
seed(0)
npts = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)
# define grid
xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,200)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')

##### PLOT
plt.figure()
CS  = plt.contour(xi,yi,zi,25,cmap=plt.cm.jet)
bar = plt.colorbar(orientation='horizontal') # draw colorbar


tick_locs   = [-3.9e-1,-2.6e-1,-1.3e-1,0e-1,1.3e-1,2.6e-1,3.9e-1]
tick_labels = ['-0.390','-0.260','-0.130','0','0.13','0.26','0.390']


bar.locator     = matplotlib.ticker.FixedLocator(tick_locs)
bar.formatter   = matplotlib.ticker.FixedFormatter(tick_labels)
bar.update_ticks()

# plot data points.
#plt.scatter(x,y,marker='o',c='b',s=5,zorder=10)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata test (%d points)' % npts)
plt.show()
16
6/27/2011 9:41:04 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon