matplotlib可拖动的图例在Mac OS 10.8上不可用吗?

时间:2013-05-01 11:42:00

标签: python macos matplotlib legend

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

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,但我不知道这是否适用于此。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

它不适用于osx后端。该后端仍然有点实验性,并且缺少若干特征(例如,子像素线宽)。但是,我刚试过,可拖动的图例适用于Qt4AggGTKAgg后端。也许你安装了其中一个?您可以通过多种方式切换后端:

import matplotlib
matplotlib.use('Qt4Agg')  

使用ipython interactive:

ipython --pylab=qt

甚至更改.matplotlibrc

backend      : Qt4Agg