Animation_matplotlib

Ruben -  
 Ruben -
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 23541 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 584
 
bonjour,
ton code est-il complet?
il me retourne des messages d'erreur: np et casteljau sont non définis.
0
Ruben
 
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 23541 Date d'inscription   Statut Contributeur Dernière intervention   1 584
 
quand je teste, rien ne s'affiche.
ton code est-il complet?
0
Ruben
 
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