Các mã sau thất bại trong việc chạy trên Python 2.5.4:AttributeError khi thêm colorbar trong matplotlib
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ác thông báo lỗi là
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'
Làm thế nào tôi có thể thêm colorbar để mã này?
Sau đây là các thông tin dịch:
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.
>>>
Một nên cẩn thận với những cái tên biến ở đây. Trong chức năng colorbar, có một từ khóa được gọi là 'cax' là viết tắt của' colorbar axes' (không phải cái bạn gọi là 'cax'), một tên tốt hơn cho kết quả của' imshow' có thể là 'img'. – pelson