Python 设置 matplotlib 颜色条范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15282189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting matplotlib colorbar range
提问by mgilson
I would like to set the matplotlib colorbar range. Here's what I have so far:
我想设置 matplotlib 颜色条范围。这是我到目前为止所拥有的:
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?
如何在此处设置颜色条限制?
采纳答案by mgilson
Arg. It's always the last thing you try:
Arg。它始终是您尝试的最后一件事:
quadmesh.set_clim(vmin=0, vmax=15)
works.
作品。
回答by The Red Gator in Virginia
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.
Matplotlib 1.3.1 - 看起来只有在实例化颜色条时才会绘制颜色条刻度。更改颜色条限制 (set_clim) 不会导致重新绘制刻度。
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.
我找到的解决方案是在与原始颜色条相同的轴条目中重新实例化颜色条。在这种情况下,轴 [1] 是原始颜色条。添加了一个新的颜色条实例,用 cax=(子轴)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()
回答by ldoyle
[Sorry, actually a comment to The Red Gator in Virginias answer, but do not have enough reputation to comment]
[对不起,实际上是对弗吉尼亚州红鳄鱼的评论,但没有足够的声誉发表评论]
I was stuck on updating the colorbar of an imshow object afterit was drawn and the data changed with imshowobj.set_data(). Using cbarobj.set_clim() indeed updates the colors, but not the ticks or range of the colorbar. Instead, you have to use imshowobj.set_clim() which will update the image and colorbar correctly.
在绘制imshow 对象并使用 imshowobj.set_data() 更改数据后,我一直坚持更新它的颜色条。使用 cbarobj.set_clim() 确实会更新颜色,但不会更新颜色条的刻度或范围。相反,您必须使用 imshowobj.set_clim() 它将正确更新图像和颜色条。
data = np.cumsum(np.ones((10,15)),0)
imshowobj = plt.imshow(data)
cbarobj = plt.colorbar(imshowobj) #adjusts scale to value range, looks OK
# change the data to some data with different value range:
imshowobj.set_data(data/10) #scale is wrong now, shows only dark color
# update colorbar correctly using imshowobj not cbarobj:
#cbarobj.set_clim(0,1) #! image colors will update, but cbar ticks not
imshowobj.set_clim(0,1) #correct

