TAGS :Viewed: 11 - Published at: a few seconds ago

[ matplotlib scatter works in ipython notebook, but not console ]

I'm trying to learn to use matplotlib and am following a tutorial to draw a scatterplot where I specify the size of the points. (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter)

import numpy as np
import matplotlib.pyplot as plt


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses

plt.scatter(x, y, s=area, alpha=0.5)
plt.show()

I am running ipython on Mac OS X using the command ipython --pylab and get the following error.

 AttributeError                            Traceback (most recent call last)
/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/figure.pyc in draw(self, renderer)
   1048         dsu.sort(key=itemgetter(0))
   1049         for zorder, a, func, args in dsu:
-> 1050             func(*args)
   1051 
   1052         renderer.close_group('figure')

/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)
   2074 
   2075         for zorder, a in dsu:
-> 2076             a.draw(renderer)
   2077 
   2078         renderer.close_group('axes')

/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/collections.pyc in draw(self, renderer)
    733     def draw(self, renderer):
    734         self.set_sizes(self._sizes, self.figure.dpi)
--> 735         Collection.draw(self, renderer)
    736 
    737 

/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/collections.pyc in draw(self, renderer)
    305                     self._linewidths, self._linestyles,
    306                     self._antialiaseds, self._urls,
--> 307                     self._offset_position)
    308 
    309         gc.restore()

/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/backends/backend_macosx.pyc in draw_path_collection(self, gc, master_transform, paths, all_transforms, offsets, offsetTrans, facecolors, edgecolors, linewidths, linestyles, antialiaseds, urls, offset_position)
     77             path_ids.append((path, transform))
     78         master_transform = master_transform.get_matrix()
---> 79         all_transforms = [t.get_matrix() for t in all_transforms]
     80         offsetTrans = offsetTrans.get_matrix()
     81         gc.draw_path_collection(master_transform, path_ids, all_transforms,

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

However, if I run the same code in an ipython notebook started using the command ipython notebook --pylab=inline, it works fine.

How can I make this work from the console?

additional info:

  • I'm running the same version of numpy on both the console and ipython notebook: numpy.version = '1.9.0.dev-7457f15'
  • The os is OSX 10.8.5
  • If I run using ipython instead of ipython --pylab, I get the same error.
  • Python 2.7.5 (default, Oct 13 2013, 13:05:22)
  • IPython 2.0.0-dev

Thanks

Answer 1


Looks like it is a backend problem on the matplotlib side. Look at the last section of error message. It indicates that something is not right in the macosx backend, which is used as the interactive backend in OSX environment.

The reason that it works under inline mode: pylab=inline confirmed this. As in the inline mode, it is not the interactive backend, but one of the non-interactive backends that is doing the work (inline backend most likely). So, it works.

The solution is probably what @Matt suggested, switch to a stable version of matplotlib