python - matplotlib scatter 在 ipython 笔记本中有效,但在控制台中无效

标签 python matplotlib ipython jupyter-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

谢谢

最佳答案

看起来这是 matplotlib 方面的后端问题。查看错误消息的最后一部分。它表示在 OSX 环境中用作交互式后端的 macosx backend 出现问题。

它在内联模式下工作的原因:pylab=inline证实了这一点。与内联模式一样,不是交互式后端,而是非交互式后端之一正在执行这项工作(最有可能是内联后端)。所以,它有效。

解决方案可能是@Matt建议的,切换到稳定版本的matplotlib

关于python - matplotlib scatter 在 ipython 笔记本中有效,但在控制台中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20821557/

相关文章:

python - ipython pandas plot不显示

python - 在 ipython 中执行 `ls` 时如何修复 PermissionError?

Python - matplotlib - 加权图

python - for - else 与 elif

python - 得到NER结果后如何获取索引?

python - 有没有办法给 matplotlib 饼图一个 zorder?

python - Django以选择形式访问对象属性

Python Tkinter : Embed a matplotlib plot in a widget

python - Matplotlib 渲染所有内部体素(带 alpha)

python - 如何使用spyder在单独的窗口中显示视频