Skip to content

Instantly share code, notes, and snippets.

@BMW009A
Created December 9, 2021 06:39
Show Gist options
  • Select an option

  • Save BMW009A/324c8ccafd74ed214d306a41e5f4a9e0 to your computer and use it in GitHub Desktop.

Select an option

Save BMW009A/324c8ccafd74ed214d306a41e5f4a9e0 to your computer and use it in GitHub Desktop.
Animation with Python matplotlib (Vehicle dynamics simulation route)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rcParams['figure.figsize']=(8.0, 8.0)
plt.rcParams['xtick.direction'] = 'in' # xtick in
plt.rcParams['ytick.direction'] = 'in' # ytick in
plt.rcParams['figure.dpi'] = 800 # resolution of figure
io = 'Constant.xlsx'
data = pd.read_excel(io=io,sheet_name=0,usecols=[0,1,2,3,4,5,6,7,8,9,10,11])
fig, ax = plt.subplots() # define figure
# plt.grid(ls='--') # define grid in figure
ax.scatter(-300, 0, color='k', linewidth= 3)
x1 = data.iloc[:,0]
x2 = data.iloc[:,2]
x3 = data.iloc[:,4]
x4 = data.iloc[:,6]
x5 = data.iloc[:,8]
x6 = data.iloc[:,10]
y1 = data.iloc[:,1]
y2 = data.iloc[:,3]
y3 = data.iloc[:,5]
y4 = data.iloc[:,7]
y5 = data.iloc[:,9]
y6 = data.iloc[:,11]
time_template = 'time = %.1fs' # make a timer
time_text = plt.text(-70, 280, '',fontsize = 25)
xdata1, ydata1 = [], []
xdata2, ydata2 = [], []
xdata3, ydata3 = [], []
xdata4, ydata4 = [], []
xdata5, ydata5 = [], []
xdata6, ydata6 = [], []
ln1, = plt.plot([], [], 'blue', animated = False, label='40km/h-State-steady', linewidth= 2)
ln2, = plt.plot([], [], 'orange', animated = False, label='40km/h-Transient', linewidth= 2)
ln3, = plt.plot([], [], 'green', animated = False, label='60km/h-State-steady', linewidth= 2)
ln4, = plt.plot([], [], 'red', animated = False, label='60km/h-Transient', linewidth= 2)
ln5, = plt.plot([], [], 'purple', animated = False, label='80km/h-State-steady', linewidth= 2)
ln6, = plt.plot([], [], 'gray', animated = False, label='80km/h-Transient', linewidth= 2)
def init(): # define the limitation of the figure
ax.set_xlim(-650, 10)
ax.set_ylim(-350, 300)
return ln1, ln2, ln3, ln4, ln5, ln6,
def animate(i): # update data will used in the plot
xdata1.append(x1[i]) # append new data to last one (because this plot is circular, both X and Y need to be updated)
ydata1.append(y1[i])
xdata2.append(x2[i])
ydata2.append(y2[i])
xdata3.append(x3[i])
ydata3.append(y3[i])
xdata4.append(x4[i])
ydata4.append(y4[i])
xdata5.append(x5[i])
ydata5.append(y5[i])
xdata6.append(x6[i])
ydata6.append(y6[i])
ln1.set_data(xdata1, ydata1) # update the data.
ln2.set_data(xdata2, ydata2) # update the data.
ln3.set_data(xdata3, ydata3) # update the data.
ln4.set_data(xdata4, ydata4) # update the data.
ln5.set_data(xdata5, ydata5) # update the data.
ln6.set_data(xdata6, ydata6) # update the data.
time_text.set_text(time_template % (0.1 * i)) # time counter
fig.canvas.draw()
return ln1, ln2, ln3, ln4, ln5, ln6,
plt.axis('off') # turn off the axis
ax.legend(frameon=False, loc="lower left") # define the label position
# animation define
ani = animation.FuncAnimation(fig=fig, func=animate, frames=1000, init_func=init, interval=5,blit=True)
ani.save('constant.gif', writer='ffmpeg', fps=60)
# To save the animation, use e.g.
plt.show()
# ani.save("movie.gif")
# or
# writer = animation.FFMpegWriter(
# fps=15, metadata=dict(artist='Me'), bitrate=1800)
# ani.save("movie.mp4", writer=writer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment