Animation de tracés en Python
Dans cet article, je vais vous montrer comment animer des tracés en Python à l'aide de Matplotlib , afin que vous puissiez ajouter une touche agréable à vos visualisations !
Matplotlib
Matplotlib est un package de traçage pour le langage de programmation Python. C'est l'un des packages les plus populaires pour les visualisations en Python et offre aux utilisateurs la possibilité de créer des tracés 2D, 3D et même animés. Matplotlib fournit une gamme d'options personnalisables pour créer des tracés, notamment des tracés linéaires, des nuages de points, des graphiques à barres et des histogrammes. Étant donné que de nombreuses personnes sont déjà familiarisées avec l'utilisation de Matplotlib pour les visualisations statiques, je couvrirai son utilisation pour les tracés animés. Pour ceux qui pourraient être nouveaux sur Matplotlib, mais qui connaissent déjà Python, voici une excellente ressource pour vous aider à démarrer sur le package.
Tracé animé 2D de base
#Empty lists for X/Y data
x = []
y = []
#Create a figure and axis to plot on
fig, ax = plt.subplots()
#We will create a function that will be called by Matplotlib's FuncAnimation
def getAnimation(iterator):
newInt = random.randint(1,10)
#Append values to our lists
x.append(iterator)
y.append(newInt)
#Clear previous data from the plot
ax.clear()
#Plot the lists with axis limits
ax.plot(x, y)
ax.set_xlim([0,20])
ax.set_ylim([0,20])
#We pass FuncAnimation our getAnimation function so that it is called in a loop
animation = FuncAnimation(fig, getAnimation, frames=20,
interval=500, repeat=False)
plt.show()
FuncAnimation() fourni via Matplotlib nécessite une figure et une fonction qui seront exécutées en boucle avec un certain nombre de cadres. En passant getAnimation() comme argument, nous créons essentiellement un diaporama de tracés qui se combinent pour former une animation, en boucle pour un certain nombre d'images. C'est le résultat de notre code...
Une animation de marche aléatoire en 3D
#Empty lists for X/Y/Z data
x = []
y = []
z = []
#Lets start off at (10,10,10) the center of our figure with axis limits 20
x.append(10)
y.append(10)
z.append(10)
#Total number of steps we will take
stepTotal = 20
#Create a figure and axis to plot on (3-D)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
#We will create a function that will be called by Matplotlib's FuncAnimation
def randomWalk(step):
#Append values to our lists
x.append(random.uniform(1,10))
y.append(random.uniform(1,10))
z.append(random.uniform(1,10))
#Clear previous data from the plot
ax.clear()
#Plot the lists with axis limits
ax.plot(x, y, z)
ax.set_xlim([0,20])
ax.set_ylim([0,20])
ax.set_zlim([0,20])
# Creating the Animation object
animation = FuncAnimation(fig, randomWalk, frames=stepTotal, interval=100)
plt.show()
Enregistrement d'animations
#FuncAnimation() returns an animation object that can be stored
animation = FuncAnimation()
#Instantiate the directory you wish to save to
directory = "/medium/animation.gif"
#We call .save() on our animation object and pass the function a directory
animation.save(directory)
Tout le code de cet article se trouve sur mon Github .
Si vous avez trouvé cet article Medium intéressant, pensez à me suivre ! J'écris sur les mathématiques, l'informatique et tout le reste !
![Qu'est-ce qu'une liste liée, de toute façon? [Partie 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































