4

Does anyone know the preferred method for stopping FuncAnimation? I am using it to record data from a oscilloscope and would like to be able to pause and restart the data on demand. Is there any way I can send a button click event to it?

Thanks, Derek

tesla
  • 51
  • 1
  • 4
  • do not think that functionality is currently implemented. You are probably better off doing the timers your self in this case. – tacaswell Feb 25 '14 at 02:57
  • Got it. So would you suggest using the TimedAnimation function then as it takes an event source which I assume means it can be stopped? I would then set up my own timers in a separate function to update the data to the figure as you said. – tesla Feb 25 '14 at 14:12
  • No, don't use the `animation` module at all (except as reference). There is (to my knowledge) no way to 'pause' running animations. – tacaswell Feb 25 '14 at 14:58
  • There is now the option to use the animation's event source, `anim.event_source.stop()` to stop the animation. See bottom two answers in [this question](https://stackoverflow.com/questions/16732379/stop-start-pause-in-python-matplotlib-animation). – ImportanceOfBeingErnest May 25 '17 at 16:52

2 Answers2

6

The FuncAnimation is a subclass of TimedAnimation. It takes frames as an input for update functionm, which could be a number or a generator. It takes repeat as an argument, which is inherited from TimedAnimation, by setting reapeat to False you could stop the animation to repeat itself.

PS: Matplotlib documentation is lame, mistakes, lazy writing style, unclear explanation. Sometime, I really have to dig into the source code to figure out what to do.

Like FuncAnimation, it also takes fargs as an extra arguments for update function, but in its documentation, it doesn't say the type of fargs. In fact, what I found in its source code, fargs is used as a list.

Hualin
  • 121
  • 1
  • 7
2

There may be some modules but I define my own pause function. It freezes the animation by clicking somewhere on the figure. When you click again, it continues to plot the animation.

You have to define a flag before the begin of your animation. Inside your animation function (the function which will be called during you animation is plotted), you have to define a a control block like: if not pause: and then write your codes for plotting the function below.

You may have already checked, but this page gives an example of what I mean. I guess, many people do it in this way.

Notice that, the switch for pause is done with the following function:

def onClick(event):
    global pause
    pause ^= True

And you have to further add this line somewhere before you call FuncAnimation

fig.canvas.mpl_connect('button_press_event', onClick)
Community
  • 1
  • 1
T-800
  • 1,543
  • 2
  • 17
  • 26