【问题标题】:Is the matplotlib draggable legend not available on Mac OS 10.8?matplotlib 可拖动图例在 Mac OS 10.8 上是否不可用?
【发布时间】:2013-05-01 11:41:59
【问题描述】:

我有一个小的 python 程序,它接收数据并使用 matplotlib (v1.3.x) 绘制它。情节有一个可拖动的传说。它在 Windows 上渲染良好,但在 Mac (OS 10.8.3) 上出现错误。代码首先创建一个图形,然后使用该图形创建一个子图,该子图返回一个轴。我正在使用轴对象来创建图例。类似这样:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
lines = ax.plot([1,2,3,4])
legend = ax.legend(lines, loc=2)
legend.draggable()
plt.show()

这是我得到的错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/legend.py", line 1007, in draggable
update=update)
File "/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/legend.py", line 54, in __init__
use_blit=use_blit)
File "/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/offsetbox.py", line 1540, in __init__
DraggableBase.__init__(self, ref_artist, use_blit=use_blit)
File "/Library/Python/2.7/site-packages/matplotlib-1.3.x-py2.7-macosx-10.8-intel.egg/matplotlib/offsetbox.py", line 1462, in __init__
self.canvas = self.ref_artist.figure.canvas
AttributeError: 'DraggableLegend' object has no attribute 'ref_artist'

我在网上没有找到任何可以解释这个错误或为什么可拖动图例在 mac 上不起作用的东西。我确实在 matplotlib 文档中找到了这个:Note that not all kinds of artist are supported by the legend yet,但我不知道这是否适用于此。

有人可以帮忙吗?

【问题讨论】:

  • 你的代码可以在我的linux机器上使用1.2.1版本的matplotlib,这是最新的stable版本

标签: python macos matplotlib legend


【解决方案1】:

它不适用于osx 后端。这个后端仍然有点实验性,它缺少几个功能(例如亚像素线宽)。但是,我刚刚尝试过,可拖动的图例适用于 Qt4AggGTKAgg 后端。也许您安装了其中之一?您可以通过多种方式切换后端:

import matplotlib
matplotlib.use('Qt4Agg')  

与 ipython 交互:

ipython --pylab=qt

甚至改变.matplotlibrc:

backend      : Qt4Agg

【讨论】:

  • 我已经尝试了您的选择,但无法让它们发挥作用。对于两个后端,我都收到有关未安装包/模块的错误(sip 和 pygtk)。收到错误后,我使用 macports 安装了这两个软件包,但我的交互式 python 似乎无法找到它们。
  • @Crystal:如果您使用的是 MacPorts,我建议您只使用 Qt4 后端——我已经广泛使用它并且在 OS X 中运行良好。您需要安装 matplotlib 的 qt4 变体,一些喜欢:port install py-matplotlib +qt4。然后,MacPorts 将安装 PyQt、sip 和所有必需的包。
  • 好的...我已经这样做了,但我仍然遇到同样的错误 - 它找不到 sip 模块。我认为我的 python 正在导入错误的 matplotlib。我已经检查以确保我正在调用正确的 python,并且确实如此。我可以在框架路径中看到所有的 matplotlib 和 Qt 包(macports 安装了 matplotlib v1.2.0),但是当我在交互式 python 中导入 matplotlib 并询问我得到 v1.3.x 的版本时。如何确保它导入正确的包?
  • 经过进一步检查,看起来 macports(或其他东西)将正确的路径附加到了 pythonpath,但由于 matplotlib 仍在先前的路径中,它首先找到 v1.3.x。
  • 终于成功了!卸载 v1.3.x,python 找到了正确的 matplotlib。谢谢!
猜你喜欢
  • 2012-09-13
  • 2013-06-24
  • 2013-05-03
  • 2011-02-02
  • 2013-07-19
  • 2013-03-03
  • 2014-06-30
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多