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

def creer_animation(f, T, nomfichier, nb_frames=100, frame_time=100):
    """
        Entrée: f : t -> (X(t), Y(t)) un couple d'array, T : array numpy (dim = 1), filename : string
        Sortie: None
        Effet: crée une animation, affichant la courbe définie par (X,Y) au cours du temps. Sauvegarde l'animation dans le fichier nomfichier.
    """
    fig, ax = plt.subplots()

    X0, Y0 = f(0)

    line, = plt.plot(X0, Y0)
    
    def animate(frame_num):
        X,Y = f(frame_num)
        line.set_data((X,Y)) #les stocke dans line
        return line

    anim = FuncAnimation(fig, animate, frames=nb_frames, interval=frame_time) #ici frames est le nombre d'images, et interval l'intervalle entre deux images dans l'animation.
    anim.save(nomfichier) #crée un fichier vidéo contenant l'animation
    return anim

