IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Enregistrer une animation matplotlib en format .mp4


Sujet :

Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2020
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2020
    Messages : 2
    Par défaut Enregistrer une animation matplotlib en format .mp4
    Bonjour à tous !

    J'ai pour projet de réaliser diverses animations et je souhaiterais les enregistrer en format .mp4

    Voici donc un code test pour m’entraîner à enregistrer :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
     
     
    plt.rcParams['animation.ffmpeg_path'] = r'C:\ffmpeg\bin\ffmpeg.exe'
     
    TWOPI = 2*np.pi
     
    fig, ax = plt.subplots()
     
    t = np.arange(0.0, TWOPI, 0.001)
    s = np.sin(t)
    l = plt.plot(t, s)
     
    ax = plt.axis([0,TWOPI,-1,1])
     
    redDot, = plt.plot([0], [np.sin(0)], 'ro')
     
    def animate(i):
        redDot.set_data(i, np.sin(i))
        return redDot,
     
     
    myAnimation = animation.FuncAnimation(fig, animate, frames=np.arange(0.0, TWOPI, 0.1), \
                                          interval=10, blit=True, repeat=False)
     
    #plt.show()
     
     
    DPI=90
    writer = animation.FFMpegWriter(fps=30, bitrate=5000)
    myAnimation.save("test1", writer = writer, dpi=DPI)
    L'animation marche très bien sans la partie #enregistrement, mais quand je l’exécute avec il y a le message d'erreur suivant que je n'arrive vraiment pas à comprendre :

    Nom : message_erreur.png
Affichages : 1656
Taille : 17,9 Ko

    Donc si quelqu'un arrivait à m'expliquer la cause de ce message et comment corriger cela je vous en serais très reconnaissant !

    PS : le format .mp4 n'est pas obligatoire, juste un format que je puisse lire avec mon ordinateur.


    Merci d'avance !

  2. #2
    Membre à l'essai

    Femme Profil pro
    Ingénieur validation
    Inscrit en
    Décembre 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 50
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur validation

    Informations forums :
    Inscription : Décembre 2015
    Messages : 5
    Billets dans le blog
    1
    Par défaut sauvegarde animation
    bonjour

    as tu resolu ton probleme ? ca m'intéresse je veux faire la meme chose !!

    merci

    sg

  3. #3
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2020
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2020
    Messages : 2
    Par défaut
    Bonjour,
    Il me semble que j'avais réussi à faire fonctionner le code en ajoutant l'extension dans le nom du fichier de sortie : "test1.mp4"

  4. #4
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut
    Je rencontre le même problème...
    As-tu résolu ton problème ?

  5. #5
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 203
    Par défaut
    hello,
    Citation Envoyé par Houthakker Voir le message
    Je rencontre le même problème...
    As-tu résolu ton problème ?
    avec le code à Maxime.f et sa remarque sur l'extension mp4 à mettre, son code fonctionne chez moi. Quel est ton problème, ton message d'erreur, ton code ?
    Ami calmant, J.P

  6. #6
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut Mon problème
    Citation Envoyé par jurassic pork Voir le message
    hello,

    avec le code à Maxime.f et sa remarque sur l'extension mp4 à mettre, son code fonctionne chez moi. Quel est ton problème, ton message d'erreur, ton code ?
    Ami calmant, J.P
    Voici mon programme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    import math
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from mpl_toolkits.mplot3d import Axes3D
     
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(111, projection="3d")
    def init():
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5)*(k-i) for i in range(k)]
        Y = [math.sin(i/5)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c=c, marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    def animate(f):
        fig.clear()
        ax = fig.add_subplot(111, projection="3d")
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5+f/10)*(k-i) for i in range(k)]
        Y = [math.sin(i/5+f/10)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c="red", marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    ani=animation.FuncAnimation(fig, animate, init_func=init, frames=90,
                                       interval=50, blit=False)
     
    DPI=90
    writer = animation.FFMpegWriter(fps=30, bitrate=5000)
    ani.save("test.mp4", writer = writer, dpi=DPI)
    et voici mon message d'erreur :

    Nom : Capture d’écran 2022-01-05 221548.png
Affichages : 1161
Taille : 63,3 Ko

  7. #7
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 203
    Par défaut
    hello,
    pour pouvoir utiliser le FFMpegWriter de animation il faut que FFMpeg soit installé dans le système d'exploitation et il faut indiquer à pyplot le chemin de l'exécutable (ffmpeg.exe).
    Exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    import matplotlib.pyplot as plt
     
     
    plt.rcParams['animation.ffmpeg_path'] = r'C:\ffmpeg\bin\ffmpeg.exe'
    Ami calmant, J.P

  8. #8
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut
    Citation Envoyé par jurassic pork Voir le message
    hello,
    pour pouvoir utiliser le FFMpegWriter de animation il faut que FFMpeg soit installé dans le système d'exploitation et il faut indiquer à pyplot le chemin de l'exécutable (ffmpeg.exe).
    Exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    import matplotlib.pyplot as plt
     
     
    plt.rcParams['animation.ffmpeg_path'] = r'C:\ffmpeg\bin\ffmpeg.exe'
    Ami calmant, J.P
    J'ai toujours un problème... Pourquoi y a-t-il un r dans l'exemple ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    import math
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from mpl_toolkits.mplot3d import Axes3D
     
    plt.rcParams['animation.ffmpeg_path'] = 'c:\users\victor\appdata\local\programs\python\python39\lib\site-packages\ffmpeg.exe'
     
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(111, projection="3d")
    def init():
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5)*(k-i) for i in range(k)]
        Y = [math.sin(i/5)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c=c, marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    def animate(f):
        fig.clear()
        ax = fig.add_subplot(111, projection="3d")
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5+f/10)*(k-i) for i in range(k)]
        Y = [math.sin(i/5+f/10)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c="red", marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    ani=animation.FuncAnimation(fig, animate, init_func=init, frames=90,
                                       interval=50, blit=False)
     
    DPI=90
    writer = animation.FFMpegWriter(fps=30, bitrate=5000)
    ani.save("test.mp4", writer = writer, dpi=DPI)
    Et voici le nouveau message d'erreur :
    Nom : Capture d’écran 2022-01-08 165149.png
Affichages : 1087
Taille : 26,4 Ko

    Amic allemand

Discussions similaires

  1. Enregistrer une animation image par image
    Par aouache dans le forum GLC_lib
    Réponses: 1
    Dernier message: 07/02/2014, 22h39
  2. Réponses: 3
    Dernier message: 25/05/2007, 22h17
  3. Enregistrer une animation pour le web
    Par paradeofphp dans le forum Flash
    Réponses: 1
    Dernier message: 01/08/2006, 14h30
  4. [VB.NET] Enregistrer une BDD sous un autre format
    Par botanique dans le forum Windows Forms
    Réponses: 9
    Dernier message: 16/01/2006, 14h42
  5. [GLScene] Comment enregistrer une image au format TGA
    Par Invité dans le forum API, COM et SDKs
    Réponses: 1
    Dernier message: 27/05/2005, 15h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo