在matplotlib中添加颜色栏时发生AttributeError [英] AttributeError while adding colorbar in matplotlib

查看:83
本文介绍了在matplotlib中添加颜色栏时发生AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法在Python 2.5.4上运行:

The following code fails to run on 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()

错误消息是

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?

以下为口译员信息:

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.
>>>

推荐答案

注意:我正在使用python 2.6.2.您的代码也出现了同样的错误,下面的修改解决了该问题.

Note: I am using python 2.6.2. The same error was raised with your code and the following modification solved the problem.

我阅读了以下颜色栏示例: http://matplotlib.sourceforge.net/examples /pylab_examples/colorbar_tick_labelling_demo.html

I read the following colorbar example: 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()

不确定您的示例为何不起作用.我对matplotlib不太熟悉.

Not sure why your example didn't work. I'm not that familiar with matplotlib.

这篇关于在matplotlib中添加颜色栏时发生AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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