[英]AttributeError while adding colorbar in matplotlib
The following code fails to run on Python 2.5.4:以下代码无法在 Python 2.5.4 上运行:
from matplotlib import pylab as pl
import numpy as np
data = np.random.rand(6,6)
fig = pl.figure(1)
fig.clf()
ax = fig.add_subplot(1,1,1)
ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
pl.colorbar()
pl.show()
The error message is错误信息是
C:\temp>python z.py
Traceback (most recent call last):
File "z.py", line 10, in <module>
pl.colorbar()
File "C:\Python25\lib\site-packages\matplotlib\pyplot.py", line 1369, in colorbar
ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
File "C:\Python25\lib\site-packages\matplotlib\figure.py", line 1046, in colorbar
cb = cbar.Colorbar(cax, mappable, **kw)
File "C:\Python25\lib\site-packages\matplotlib\colorbar.py", line 622, in __init__
mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
AttributeError: 'NoneType' object has no attribute 'autoscale_None'
How can I add colorbar to this code?如何向此代码添加颜色条?
Following is the interpreter information:以下是口译员信息:
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
(This is a very old question I know) The reason you are seeing this issue is because you have mixed the use of the state machine (matplotlib.pyplot) with the OO approach of adding images to an axes. (我知道这是一个非常古老的问题)您看到这个问题的原因是因为您将状态机 (matplotlib.pyplot) 与向轴添加图像的 OO 方法混合使用。
The plt.imshow
function differs from the ax.imshow
method in just one subtly different way. plt.imshow
函数与ax.imshow
方法的不同之处仅在于一种微妙的不同。 The method ax.imshow
:方法
ax.imshow
:
The function plt.imshow
:函数
plt.imshow
:
plt.colorbar
function).plt.colorbar
函数自动拾取)。 If you want to be able to use the plt.colorbar
(which in all but the most extreme cases, you do) with the ax.imshow
method, you will need to pass the returned image (which is an instance of a ScalarMappable
) to plt.colorbar
as the first argument:如果你希望能够使用
plt.colorbar
与(在所有,但最极端的情况下,你做的) ax.imshow
方法,你就需要通过返回的图像(这是一个实例ScalarMappable
)至plt.colorbar
作为第一个参数:
plt.imshow(image_file)
plt.colorbar()
is equivalent (without using the state machine) to:等效于(不使用状态机):
img = ax.imshow(image_file)
plt.colorbar(img, ax=ax)
If ax is the current axes in pyplot, then the kwarg ax=ax
is not needed.如果 ax 是 pyplot 中的当前轴,则不需要 kwarg
ax=ax
。
Note: I am using python 2.6.2.注意:我使用的是 python 2.6.2。 The same error was raised with your code and the following modification solved the problem.
您的代码出现了同样的错误,以下修改解决了该问题。
I read the following colorbar example: http://matplotlib.sourceforge.net/examples/pylab_examples/colorbar_tick_labelling_demo.html我阅读了以下颜色条示例: http : //matplotlib.sourceforge.net/examples/pylab_examples/colorbar_tick_labelling_demo.html
from matplotlib import pylab as pl
import numpy as np
data = np.random.rand(6,6)
fig = pl.figure(1)
fig.clf()
ax = fig.add_subplot(1,1,1)
img = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
fig.colorbar(img)
pl.show()
Not sure why your example didn't work.不知道为什么你的例子不起作用。 I'm not that familiar with matplotlib.
我对 matplotlib 不太熟悉。
I found another solution to this problem in a tutorial.我在教程中找到了另一个解决此问题的方法。
The code below, will work well for the plt.imshow() method:下面的代码适用于 plt.imshow() 方法:
def colorbar(Mappable, Orientation='vertical', Extend='both'):
Ax = Mappable.axes
fig = Ax.figure
divider = make_axes_locatable(Ax)
Cax = divider.append_axes("right", size="5%", pad=0.05)
return fig.colorbar(
mappable=Mappable,
cax=Cax,
use_gridspec=True,
extend=Extend, # mostra um colorbar full resolution de z
orientation=Orientation
)
fig, ax = plt.subplots(ncols=2)
img1 = ax[0].imshow(data)
colorbar(img1)
img2 = ax[1].imshow(-data)
colorbar(img2)
fig.tight_layout(h_pad=1)
plt.show()
It may not work well with other plotting methods.它可能不适用于其他绘图方法。 For example, it did not work with Geopandas Geodataframe plot.
例如,它不适用于 Geopandas Geodataframe 图。
Add/edit the following lines to your code将以下行添加/编辑到您的代码中
plot = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
pl.colorbar(plot)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.