2016-03-24 68 views
0

我是一個新手程序員,我的直覺是,這個錯誤是由於某種安裝或版本問題,但我不知道是什麼。我在OS 10.8上運行python 2.7,今天剛安裝numpy 1.12.0和matplotlib-1.5.1,試圖構建一個熱圖。AttributeError:'numpy.ndarray'對象沒有屬性'as_rgba_str'

我試圖運行從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' 
+0

'as_rgba_str'是'image'的一種方法; http://stackoverflow.com/questions/31393769/getting-an-rgba-array-from-a-matplotlib-image/31396968#31396968;你應該顯示一些發生錯誤的堆棧(行)。這可能會讓您知道安裝中缺少的內容。 – hpaulj

回答

1

肯定會出現某種安裝錯誤,我安裝了Anaconda並解決了問題。對於那些未來會發現這個問題的人來說,ActivePython沒有numpy或scipy。

-1

我無法解決您的問題,但我可以告訴你,它的安裝問題,您發佈的代碼工作完美對我的安裝使用Python 2.7。雖然我使用Windows而不是IOS

+0

是的,這就是我所害怕的。感謝您檢查我!如果它太讓人沮喪,我只需切換到實驗室中的Linux機器。 –

+0

您是否嘗試安裝直接與這些科學模塊直接相關的python版本 - http://www.activestate.com/activepython/downloads – zorg93

+0

將嘗試使用其中一個版本重新安裝python會導致任何問題?我應該嘗試先卸載當前的python發行版嗎? –