Gtk blit animation, best practices

HI to the list,

I'm playing with blit animations in a gtk window. Looking at MPL
examples I came up with a working example (attached) that shows a sin
wave with a play toggle button and a "scale" button that changes the
axis limits. In order to plot the first frame (before play is clicked)
and to correctly implement the scale action I've written the "refresh"
method that follows:

    def refresh(self):
        # This explicit draw is needed to draw the grid and to save a clean
        # background
        self.canvas.draw()
        self.background = self.canvas.copy_from_bbox(self.ax.bbox)

        # This draw is needed to draw the stand-still plot (first frame)
        self.line.set_animated(False)
        self.canvas.draw()
        self.line.set_animated(True)
            # NOTE: Saving the background here would save the line too so I have
            # to call canvas.draw() two times

This (quite redundant) implementation works. However, (QUESTION 1) is
it expected that the following straightforward implementation:

    def refresh(self):
        self.line.set_animated(False)
        self.canvas.draw()
        self.line.set_animated(True)
        self.background = self.canvas.copy_from_bbox(self.ax.bbox)

saves in background also the line? So in this case I would have the
animation superimposed to the first frame? Seems like a bug...

Is there a better way to achieve the same result?

Furthermore, I want to attach the refresh method to some events in
order to refresh the plot event if play is not active. For example
zooming or panning with the matplotlib toolbar buttons makes the plot
disappear. (QUESTION 2) Is there such event I can use to have the line
plotted? Or is better to implement this changing from an animated to a
still state? In this latter case once I plot with animated=False I'm
not able anymore to save a clean background without the line (event
using the line.set_animated(True) method before copying the bg).

When we have solved this file could be a nice reference example.

Thanks,
    ~ Antonio

gtk_animation_refresh.py (4.1 KB)