Could animation be triggered only when an onclick event in Silder?

Hi, I have a question about could animation be triggered only when an onclick event in Silder? For example, there’s a Slider object which represents the frequency. When I click on the object and choose on one frequency value, a sin curve will be shown and also there’s a moving dot on the curve which will show an animation about the moving dot.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import Slider

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)

axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor='lightgoldenrodyellow')
sfreq = Slider(axfreq, 'Freq', 0.1, 20.0, valinit=3, valstep=5.0)

x = np.linspace(0, 5, 100)
y = np.sin(3 * x)
l, = ax.plot(x, y)

node, = ax.plot([x[0]], [y[0]], 'bo')

def animate(x, freq):
    y = np.sin(freq * x)
    node.set_data(x, y)
    return node


def update(val):
    freq = sfreq.val
    l.set_ydata(np.sin(freq * x))
    FuncAnimation(fig, animate, frames=np.linspace(0, 5, 100), fargs=(freq, ), repeat=False)
    fig.canvas.draw_idle()

sfreq.on_changed(update)

plt.show()

However the code above doesn’t show the animation but only a sin curve and the initial dot on it. Are there some examples? Thank you.

I don’t know about the rest of your issue, but you need to save this to a variable or your animation will be deleted, as noted in the animation API docs.

Thanks for your responce. I have tried ani = FuncAnimation(fig, animate, frames=np.linspace(0, 5, 100), fargs=(freq, ), repeat=False) but it seems that it doesn’t work.

That would be thrown away when the function exits; it needs to be saved in a global variable or some class instance attribute that is held on to always.

Thank you very much.