【问题标题】:update an artiste (Line2D) on a figure without re-drawing the whole figure - axes.draw_artist() crashes在不重新绘制整个图形的情况下更新图形上的艺术家 (Line2D) - axes.draw_artist() 崩溃
【发布时间】:2013-11-30 00:21:46
【问题描述】:

我想要一个线 (Line2D) 对象随着当前光标位置在图形的多个轴上移动。我现在这样做的方式是在每次光标移动时重新绘制整个图形,通过调用

fig.canvas.draw()

我的图由14个面板组成,其中10个有一条必须随光标移动的线,这样做很长。

我想知道如何更新线条而不重绘整个内容。

所以我尝试使用

ax.draw_artist(line)

但这会崩溃。这是一个小例子:

fig,ax =  subplots()
line = Line2D([0.3,0.3],[0.1,0.8])
ax.add_artist(line)
fig.canvas.draw()

#here I see the line on the figure ok

# I update the position of the line
line.set_xdata([0.6,0.8])

# now draw the line (this should add another one (*))
ax.draw_artist(line)

最后一行导致以下错误:

----------------------------------- ---------------------------- RuntimeError Traceback(最近调用 最后)在() ----> 1 ax.draw_artist(line)

/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc 在 draw_artist(self, a) 2096 """ 2097 断言 self._cachedRenderer 不是 None -> 2098 a.draw(self._cachedRenderer) 2099 2100 def redraw_in_frame(self):

/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc 在draw_wrapper(艺术家,渲染器,*args,**kwargs) 53 def draw_wrapper(艺术家,渲染器,*args,**kwargs): 54 之前(艺术家、渲染师) ---> 55 绘制(艺术家、渲染器、*args、**kwargs) 56 后(艺术家、渲染师) 57

/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/lines.pyc 在绘图(自我,渲染器) 524 第525章 --> 526 gc = renderer.new_gc() 第527章 528

/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.pyc 在 new_gc(self) 95 96 def new_gc(自我): ---> 97 self.gc.save() 98 self.gc.set_hatch(无) 99 self.gc._alpha = 1.0

RuntimeError: CGContextRef 为 NULL

我不确定这个错误来自哪里??

另外,我发现我应该在绘制第一条线之前以某种方式保存画布并在每次重新绘制时恢复它,所以只出现一条线。我已经看到这可以通过:

 background = canvas.copy_from_bbox(ax.bbox)

并使用恢复它

canvas.restore_region(background)

但是我在对象“画布”中没有这两种方法。这是 MacOSX 后端的问题吗?

我在 macOS 10.9 上安装了 matplotlib 1.3.1 和 anaconda 发行版。

我也被告知我可以使用:

fig.canvas.blit(line.clipbox)

在它的新位置画线,但这绝对没有任何作用。

【问题讨论】:

  • OSX 后端有很多限制。正如您所注意到的,它不支持 blitting。 (另见:github.com/matplotlib/matplotlib/issues/531)老实说,除非你真的需要它,否则最好避免使用 OSX 后端。
  • 这就是我开始的想法......我不确定如何使用另一个后端。我已经尝试过:例如将 matplotlib 作为 mplt mpl.use('Qt4Agg') 导入 .. 这也给了我错误(缺少 'sip',顺便说一下无法安装)
  • 这取决于你如何安装 matplotlib。如果它们各自的 gui 工具包可用,则构建交互式后端(例如 TkAgg、OSX、Qt4Agg 等)。您最好的选择是 TkAgg 后端(大多数平台上的默认设置)。但是,Apple 的 Tk 经常会出现问题(我不记得细节了),因此您的 python 安装可能没有 Tk 可用。对于其他后端,您需要安装 gui 工具包(以及它的 python 绑定)并重建 matplotlib。 (OSX 上的 Matplotlib 比它应该的更痛苦。)
  • 我的 matplotlib 带有“anaconda”发行版 (docs.continuum.io/anaconda)。不确定包含什么后端

标签: python macos matplotlib scipy


【解决方案1】:

所以我找到的唯一解决方案是放弃 anaconda 并使用 macports 和后端 qt4 重新安装所有内容。

sudo port install py27-matplotlib +qt4

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 2011-07-07
    • 2013-08-08
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多