0

I have an animation that I can play and pause as desired. As shown in the code below, my method for pausing the animation is to intermittently poll the paused global state variable.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from mpl_toolkits.mplot3d import proj3d

paused = False

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_ylim(-100, 100)
ax.set_xlim(-10, 10)
ax.set_zlim(-100, 100)

plt.ion()
plt.show()

def pause_anim(event):
    global paused
    paused = not paused

pause_ax = fig.add_axes((0.7, 0.03, 0.1, 0.04))
pause_button = Button(pause_ax, 'pause', hovercolor='0.975')
pause_button.on_clicked(pause_anim)

x = np.arange(-50, 51)

line = ax.plot([], [], [], c="r")[0]


for y in np.arange(1, 30, 3):
    z = - x**2 + y - 100

    line.set_data(x, 0)
    line.set_3d_properties(z)

    plt.draw()
    plt.pause(0.2)

    while paused:
        plt.pause(0.5)

Is there some way to pause the animation without polling and instead use some sort of interrupt driven method?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
  • As said in the [previous answer](http://stackoverflow.com/a/43385128/4124317), `FuncAnimation` has the methods `.event_source.stop()` and `.event_source.start()`. Since `FuncAnimation` would anyways allow for much cleaner solutions, I wonder why you don't want to use it. Could it be that the problem you are trying to solve in reality is a completely different one? – ImportanceOfBeingErnest Apr 13 '17 at 08:21
  • The cost of refactoring my code to use `FuncAnimation` is significant, so that's why I'm doing the animation part manually. In another way, I'm also trying to get a better understanding of Matplotlib's internals. Don't feel obliged to answer this question, I'm still considering whether to delete it or not. – Seanny123 Apr 13 '17 at 09:28

0 Answers0