Animation_matplotlib

Fermé
Ruben - 26 avril 2021 à 15:53
 Ruben - 26 avril 2021 à 18:35
Bonjour,

Sur le graphe de mon animation j'ai besoin d'afficher des points fixes, points de contrôle de la courbe. Mais quand j'appel la méthode scatter avec les coordonnées des points seulement le premier (4,2) s'affiche...

Voici mon code :
P=np.array([[4.,2.,.0],[0.,-1,0.],[0.,4,0],[4.,1,0]], dtype = float)
    
t = np.linspace(0,1,1000)
x=[]
y=[]

for t in t:
    p=casteljau(P,t)
    x.append(p[0])
    y.append(p[1])


fig, ax=plt.subplots(figsize=(8, 8))

ax.set(xlim=(-1,4), ylim=(-1,4))



line, = ax.plot([],[])

ax.scatter(P[:,0],P[:,1], color="red", label="Control Points")

ax.set_xlabel('x')
ax.set_ylabel('y')

def animate(i):
    line.set_data(x[:i],y[:i])
    return line,


anim = animation.FuncAnimation(fig, animate, frames = len(x)+1, interval =50, blit= True)


Quelqu'un pour m'aider ? ;)


Configuration: Windows / Edge 90.0.818.46

3 réponses

yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 1 471
26 avril 2021 à 16:33
bonjour,
ton code est-il complet?
il me retourne des messages d'erreur: np et casteljau sont non définis.
0
oui autant pour moi, voici la fonction casteljau :
def casteljau(P,t):
    n=len(P)
    temp=P
    
    for r in range(n-1,0,-1):
        for i in range(r):
            temp[i]=(1-t)*temp[i]+t*temp[i+1]
         
     
    return temp[0]


Et les bibliothèques utilisées :
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
0
yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 1 471
26 avril 2021 à 17:41
quand je teste, rien ne s'affiche.
ton code est-il complet?
0
Alors je remets le code en entier : (Chez moi l'animation se lance normalement il manque juste les points de contrôles comme j'expliquais au début)

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


P=np.array([[4.,2.,.0],[0.,-1,0.],[0.,4,0],[4.,1,0]], dtype = float)
    
t = np.linspace(0,1,1000)
x=[]
y=[]

for t in t:
    p=casteljau(P,t)
    x.append(p[0])
    y.append(p[1])


fig, ax=plt.subplots(figsize=(8, 8))

ax.set(xlim=(-1,4), ylim=(-1,4))



line, = ax.plot([],[])

ax.scatter(P[:,0],P[:,1], color="red", label="Control Points")

ax.set_xlabel('x')
ax.set_ylabel('y')

def animate(i):
    line.set_data(x[:i],y[:i])
    return line,


anim = animation.FuncAnimation(fig, animate, frames = len(x)+1, interval =50, blit= True)


Plus la fonction casteljau :
def casteljau(P,t):
    n=len(P)
    temp=P
    
    for r in range(n-1,0,-1):
        for i in range(r):
            temp[i]=(1-t)*temp[i]+t*temp[i+1]
         
     
    return temp[0]
0