Matplotlib panel animation - close leaves of old frames behind

I have a wxPython application that contains a matplotlib panel (kindly provided by wxmpl, although I saw the same with a simple FigureCanvasWxAgg canvas).

I would like to revive one of the plots in the panel, and in the past I have done similar things. The way I do this is suggested:

  • copy background
  • plot
  • [...]
  • restore background
  • update line data
  • draw artist
  • Blit

The problem is that the stories, instead of being “rewritten” by restoring the background, remain there, and all things, for obvious reasons, look messy.

Some (simplified) code:

fig = self.myPanel.get_figure()
ax_top = fig.add_subplot(211)
ax_bottom = self.fig.add_subplot(212)
canvas = fig.canvas
canvas.draw()
bg_top = canvas.copy_from_bbox(ax_top.bbox)
bg_bottom = canvas.copy_from_bbox(ax_bottom.bbox)
line, = ax_bottom.plot(x, y, 'k', animated=True)

Then, when updating:

canvas.restore_region(bg_bottom)
line.set_ydata(new_y)
ax_bottom.draw_artist(line)
canvas.blit(ax_bottom.bbox)

A new line is drawn (and very fast! :), but for some reason this happens on the old line. Can anyone guess why?

+3
3

, :)

fig.canvas.draw() fig.canvas.copy_from_bbox. , , , , , .

+4

FigureCanvasWxAgg. , , , - . , , , /:

...
bg_top = None
bg_bottom = None
line, = ax_bottom.plot(x, y, 'k', animated=True)
...

:

def update(self, evt):
    if bf_top is None:
        bg_top = canvas.copy_from_bbox(ax_top.bbox)
        bg_bottom = canvas.copy_from_bbox(ax_bottom.bbox)
    canvas.restore_region(bg_bottom)
    line.set_ydata(new_y)
    ax_bottom.draw_artist(line)
    canvas.blit(ax_bottom.bbox)
+2

You must associate draw_event with a new copy of the background. Otherwise, the old background will always be on the desired background, and you can only get the scale or pan on the toolbar. This works for me.

Martin

0
source

All Articles