matplotlib分散在ipython笔记本中工作,但不在控制台中

时间:2013-12-29 02:33:52

标签: python matplotlib ipython ipython-notebook

我正在尝试学习使用matplotlib并遵循教程绘制散点图,我指定了点的大小。 (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()

我使用命令ipython --pylab在Mac OS X上运行ipython并收到以下错误。

 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'

但是,如果我在使用命令ipython notebook --pylab=inline开始的ipython笔记本中运行相同的代码,它可以正常工作。

如何从控制台进行此操作?

其他信息:

  • 我在控制台和ipython上运行相同版本的numpy 笔记本:numpy。版本 ='1.9.0.dev-7457f15'
  • 操作系统是OSX 10.8.5
  • 如果我使用ipython代替ipython --pylab,我会得到相同的结果 错误。
  • Python 2.7.5(默认,2013年10月13日,13:05:22)
  • IPython 2.0.0-dev

由于

1 个答案:

答案 0 :(得分:2)

backend方看起来是matplotlib问题。查看错误消息的最后一部分。它表示macosx backend中的某些内容不正确,OSX pylab=inline用作inline环境中的交互式后端。

它在内联模式下工作的原因:matplotlib证实了这一点。与内联模式一样,它不是交互式后端,而是一个正在进行工作的非交互式后端(最有可能是{{1}}后端)。所以,它的工作原理。

解决方案可能是@Matt建议的,切换到{{1}}

的稳定版本