0

I'm a novice programmer and my hunch is that this error is due to some sort of installation or version problem, but I have no idea what. I'm running python 2.7 on OS 10.8, and just installed numpy 1.12.0 and matplotlib-1.5.1 today in an attempt to construct a heatmap.

I'm trying to run this example from the matplotlib site (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()

A plot window appears, but nothing is displayed, and the mouseover coordinates kind of "stack" instead of refreshing and quickly become illegible. I also get this error in terminal:

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

Other, similar examples from the matplotlib site also exhibit similar behavior. Of course, please let me know if this is a duplicate (I tried to search for an answer but didn't find anything similar to my problem, but I also might just not know what to search for).

If it is an installation error, instructions on how to fix it or a point in the right direction with detailed instructions would be much appreciated. Thank you!

Edit: Here's the traceback before the error:

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'
Nick E.
  • 11
  • 5
  • `as_rgba_str` is a method of an `image`; http://stackoverflow.com/questions/31393769/getting-an-rgba-array-from-a-matplotlib-image/31396968#31396968; you should show some of the stack (lines) where the error occurs. That may give an idea of what is missing in your installation. – hpaulj Mar 25 '16 at 00:12

2 Answers2

1

Definitely some sort of install error, I installed Anaconda and the problem was fixed. For those who find this question in the future, ActivePython does not have numpy or scipy.

Nick E.
  • 11
  • 5
-1

I cant fix your problem but I can tell you its an installation problem, the code you posted worked perfect on my install using python 2.7. though I am using windows instead of IOS

zorg93
  • 39
  • 3
  • Yeah, that's what I was afraid of. Thanks for checking for me! If it gets too frustrating I'll just switch over to the linux machines in my lab. – Nick E. Mar 25 '16 at 00:14
  • have you tried installing a version of python that comes directly with those scientific modules - http://www.activestate.com/activepython/downloads – zorg93 Mar 25 '16 at 00:23
  • will attempting to reinstall python using one of those versions cause any problems? Should I try to uninstall my current python distribution first? – Nick E. Mar 25 '16 at 00:33
  • Yes, otherwise you probably get warning when trying to install. Also if you are currently using 2.7 there is no need to go to a later version of python – zorg93 Mar 25 '16 at 00:35