AttributeError:'numpy.ndarray'对象没有属性'as_rgba_str'

时间:2016-03-24 23:59:30

标签: python numpy matplotlib

我是一名新手程序员,我的预感是这个错误是由于某种安装或版本问题,但我不知道是什么。我在OS 10.8上运行python 2.7,今天刚刚安装了numpy 1.12.0和matplotlib-1.5.1,试图构建一个热图。

我正在尝试从matplotlib网站(http://matplotlib.org/examples/api/image_zcoord.html)运行此示例:

"""
Show how to modify the coordinate formatter to report the image "z"
value of the nearest pixel given x and y
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

X = 10*np.random.rand(5, 3)

fig, ax = plt.subplots()
ax.imshow(X, cmap=cm.jet, interpolation='nearest')

numrows, numcols = X.shape


def format_coord(x, y):
    col = int(x + 0.5)
    row = int(y + 0.5)
    if col >= 0 and col < numcols and row >= 0 and row < numrows:
        z = X[row, col]
        return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, z)
    else:
        return 'x=%1.4f, y=%1.4f' % (x, y)

ax.format_coord = format_coord
plt.show()

出现一个绘图窗口,但没有显示任何内容,并且鼠标悬停会协调一种“堆叠”而不是刷新,并且很快变得难以辨认。我也在终端中收到此错误:

AttributeError: 'numpy.ndarray' object has no attribute 'as_rgba_str'

其他来自matplotlib网站的类似例子也表现出类似的行为。 当然,如果这是重复的,请告诉我(我试图找到答案,但没有发现任何类似于我的问题,但我也可能只是不知道要搜索什么)。

如果是安装错误,将非常感谢有关如何使用详细说明修复它或指向正确方向的指示。谢谢!

编辑:这是错误之前的追溯:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-    py2.7-macosx-10.8-x86_64.egg/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/figure.py", line 1262, in draw
renderer, self, dsu, self.suppressComposite)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/axes/_base.py", line 2355, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/image.py", line 472, in draw
renderer.draw_image(gc, l, b, im)
  File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/backends/backend_macosx.py", line 113, in draw_image
    nrows, ncols, data = im.as_rgba_str()
AttributeError: 'numpy.ndarray' object has no attribute 'as_rgba_str'

2 个答案:

答案 0 :(得分:1)

肯定是某种安装错误,我安装了Anaconda,问题得到解决。对于那些在将来发现这个问题的人来说,ActivePython没有numpy或scipy。

答案 1 :(得分:-1)

我无法解决您的问题,但我可以告诉您它的安装问题,您发布的代码在我使用python 2.7进行安装时非常完美。虽然我使用的是Windows而不是IOS

相关问题