在不重新绘制整个图形的情况下更新图形上的artiste(Line2D)-axes.draw_artist()崩溃 [英] update an artiste (Line2D) on a figure without re-drawing the whole figure - axes.draw_artist() crashes
问题描述
我希望线(Line2D)对象以图形的多个轴上的当前光标位置移动.我现在做的方法是每次移动光标时通过调用
I want a line (Line2D) object to move with the current cursor position on several axes of a figure. The way I'm doing it now is to re-draw the whole figure each time the cursor moves, by calling
fig.canvas.draw()
我的图形由14个面板组成,其中10个面板有一条必须随光标移动的线,并且这样做很长.
My figure consists in 14 panels among which 10 has a line which must move with the cursor, and doing this is very long.
我想知道如何仅更新行,而不会重绘整个内容.
I was wondering how to update only the lines and not redrawing the whole thing.
所以我尝试使用
ax.draw_artist(line)
但是这崩溃了.这是一个小例子:
but this crashes. Here is a small example :
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)
最后一行会导致以下错误:
this last line causes the following error:
--------------------------------------------------- ---------------------------- RuntimeError Traceback(最近一次调用 最后)在() ----> 1个ax.draw_artist(行)
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) in () ----> 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/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 在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/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 在draw中(自己,渲染器) 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/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 在new_gc()中 95 96 def new_gc(): ---> 97 self.gc.save() 98 self.gc.set_hatch(无) 99 self.gc._alpha = 1.0
/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为NULL
RuntimeError: CGContextRef is NULL
我不确定此错误来自何处?
I'm not sure where this error comes from??
此外,我已经看到我应该以某种方式在绘制第一行之前保存画布,并在每次重新绘制时将其还原,因此仅出现一行.我已经看到可以使用:
Also, I've seen that I was supposed to somehow save the canvas before the first line is drawn and restore it each time I re draw it, so only one line appears. I've seen this could be done with :
background = canvas.copy_from_bbox(ax.bbox)
并使用
canvas.restore_region(background)
但是我在对象'canvas'中都没有这两种方法. MacOSX后端有问题吗?
however I have neither of these two methods in the object 'canvas'. Is this a problem with the MacOSX backend?
我在macOS 10.9上的发行版anaconda上安装了matplotlib 1.3.1.
I have matplotlib 1.3.1 installed with the distribution anaconda on macOS 10.9.
我也被告知可以使用:
fig.canvas.blit(line.clipbox)
在新位置画线,但这绝对没有任何作用.
to draw the line at its new position, but that does absolutely nothing.
推荐答案
所以我发现的唯一解决方案是放弃anaconda,并使用macports和后端qt4重新安装所有内容.
so the only solution I found is to give up anaconda and re-install everything with macports and with the backend qt4.
sudo port install py27-matplotlib +qt4
这篇关于在不重新绘制整个图形的情况下更新图形上的artiste(Line2D)-axes.draw_artist()崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!