我刚找到解决办法。必须打电话bar.update_ticks()
更改格式化程序/定位程序后,请参见
然后一切正常。
更新:
这也是更改格式化程序/定位程序的代码。它基于彩色条码的内部结构,所以也许其他人有更好的解决方案:#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()