我想要一个线 (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 (most recent call last) in () ----> 1 ax.draw_artist(line)
/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in draw_artist(self, a) 2096 """ 2097 assert self._cachedRenderer is not None -> 2098 a.draw(self._cachedRenderer) 2099 2100 def redraw_in_frame(self):
/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs) 53 def draw_wrapper(artist, renderer, *args, **kwargs): 54 before(artist, renderer) ---> 55 draw(artist, renderer, *args, **kwargs) 56 after(artist, renderer) 57
/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/lines.pyc in draw(self, renderer) 524 525 renderer.open_group('line2d', self.get_gid()) --> 526 gc = renderer.new_gc() 527 self._set_gc_clip(gc) 528
/Users/Heimdall/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.pyc in new_gc(self) 95 96 def new_gc(self): ---> 97 self.gc.save() 98 self.gc.set_hatch(None) 99 self.gc._alpha = 1.0
RuntimeError: CGContextRef is NULL
我不确定这个错误是从哪里来的??
此外,我已经看到我应该在绘制第一条线之前以某种方式保存 Canvas 并在每次重新绘制时恢复它,所以只出现一条线。我已经看到这可以通过以下方式完成:
background = canvas.copy_from_bbox(ax.bbox)
并用
恢复它canvas.restore_region(background)
但是我在对象“canvas”中没有这两种方法。这是 MacOSX 后端的问题吗?
我在 macOS 10.9 上安装了 matplotlib 1.3.1 和发行版 anaconda。
我也被告知我可以使用:
fig.canvas.blit(line.clipbox)
在它的新位置画线,但这绝对没有任何作用。
最佳答案
所以我找到的唯一解决方案是放弃 anaconda 并使用 macports 和后端 qt4 重新安装所有内容。
sudo port install py27-matplotlib +qt4
关于python - 在不重新绘制整个图形的情况下更新图形上的艺术家 (Line2D) - axes.draw_artist() 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20020882/