python matplotlib colorbar设置刻度格式或定位器更改刻度标签 [英] python matplotlib colorbar setting tick formator/locator changes tick labels

查看:58
本文介绍了python matplotlib colorbar设置刻度格式或定位器更改刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

个用户,我想自定义颜色条上的刻度.但是,我发现以下奇怪行为.我尝试将刻度格式器更改为默认格式器(我认为这应该什么都不会更改),但最终得到了不同的标签.有人知道我在做什么错吗?还是这是一个错误?

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?

我使用git(v1.0.1-961-gb516ae0,git describe)中的matplotlib.以下代码生成了两个图:

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

推荐答案

我刚刚找到了解决方案.一个必须打电话

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

然后一切正常.

更新:

这也是更改Formator/Locator的代码.它基于颜色条码的内部结构,因此也许其他人有更好的解决方案:

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

这篇关于python matplotlib colorbar设置刻度格式或定位器更改刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆