# L-18 MCS 507 Mon 2 Oct 2023 : animatedsine.py

"""
Illustration of the use of the FuncAnimation of matplotlib
to define an animation of sine function with increasing frequencies,
and to make a gif file with imagemagick.
"""

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

def animate(freq):
    """
    Makes a plot of the sine function
    for the given frequence in freq.
    """
    x = np.linspace(0, 2*np.pi, 100)
    y = np.sin((freq+1)*x) # first call is with 0
    plt.clf() # clears the current plot
    axs = plt.axes(xlim=(0, 2*np.pi), ylim=(-1.5, 1.5))
    plt.plot(x, y)

fig = plt.figure()
anim = animation.FuncAnimation(fig, animate, frames=5)
anim.save('animatedsine.gif', writer='imagemagick')

plt.show()
