I have a wxPython application which contains a matplotlib panel (courtesy of wxmpl, though I’ve seen the same with a plain FigureCanvasWxAgg canvas).
I’d want to animate one of the plots in the panel, and in the past I’ve made similar things happen. The way I’m doing it is the suggested:
- copy the background
- plot
- […]
- restore background
- update line data
- draw artist
- blit
The problem is that the plots, instead of being “overwritten” by the background restoring, stay there and the whole things understandably looks a mess.
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, on update:
canvas.restore_region(bg_bottom)
line.set_ydata(new_y)
ax_bottom.draw_artist(line)
canvas.blit(ax_bottom.bbox)
The new line gets drawn (and very fast! :), but for some reason it happens over the old line. Can anybody guess why?
Added as an answer, by request 🙂
Try calling
fig.canvas.draw()before callingfig.canvas.copy_from_bbox. The exact behavior depends on the backend, so it will be different on different platforms, but generally speaking you need to draw the canvas before trying to copy things from it.