Telemetry Video #85
-
Hello, is there a way to use MoviePY library to generate a video of telemetry? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 1 reply
-
There should be a way with MatPlotLib to animate a dataframe, you can look up to https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ |
Beta Was this translation helpful? Give feedback.
-
@marinik98 Ok, will try. I no nothing about matplotlib, animation etc. |
Beta Was this translation helpful? Give feedback.
-
Even this documentation can be useful in your case, never tried to implement any animation with the telemetries but can't be that obnoxious: https://plotly.com/python/animations/ |
Beta Was this translation helpful? Give feedback.
-
@marinik98 Sorry, I tried but I cannot. Can you mind checking what is the problem. """
Matplotlib Animation Example
author: Jake Vanderplas
email: [email protected]
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import fastf1 as ff1
from fastf1 import plotting
# First set up the figure, the axis, and the plot element we want to animate
plotting.setup_mpl()
graph1 = plt.figure()
ff1.Cache.enable_cache('cache') # optional but recommended
year = 2021
wknd = 22
ses = 'R'
drv1 = 'VER'
drv2 = 'VER'
weekend = ff1.get_session(year, wknd)
session = ff1.get_session(year, wknd, ses)
laps = session.load_laps(with_telemetry=True)
first_driver = laps.pick_driver(drv1)
first_driver_info = session.get_driver(drv1)
first_color = plotting.team_color(first_driver_info.team)
second_driver = laps.pick_driver(drv2)
second_driver_info = session.get_driver(drv2)
second_color = plotting.team_color(second_driver_info.team)
first_driver = laps.pick_driver(drv1).pick_fastest()
first_car = first_driver.get_car_data()
second_driver = laps.pick_driver(drv2).pick_fastest()
second_car = second_driver.get_car_data()
fig, ax = plt.subplots(6)
fig.suptitle(f'{first_driver_info.familyname} vs {second_driver_info.familyname} - Fastest lap - {weekend.name} {year} {ses}ualifying', size=18)
l2, = ax[0].plot(second_car['Time'], second_car['Speed'], color=second_color)
l1, = ax[0].plot(first_car['Time'], first_car['Speed'], color=first_color)
ax[1].plot(second_car['Time'], second_car['RPM'], color=second_color)
ax[1].plot(first_car['Time'], first_car['RPM'], color=first_color)
ax[2].plot(second_car['Time'], second_car['nGear'], color=second_color)
ax[2].plot(first_car['Time'], first_car['nGear'], color=first_color)
ax[3].plot(second_car['Time'], second_car['Throttle'], color=second_color)
ax[3].plot(first_car['Time'], first_car['Throttle'], color=first_color)
ax[4].plot(second_car['Time'], second_car['Brake'], color=second_color)
ax[4].plot(first_car['Time'], first_car['Brake'], color=first_color)
ax[5].plot(second_car['Time'], second_car['DRS'], color=second_color)
ax[5].plot(first_car['Time'], first_car['DRS'], color=first_color)
ax[0].set_ylabel("Speed [km/h]")
ax[1].set_ylabel("RPM [#]")
ax[2].set_ylabel("Gear [#]")
ax[3].set_ylabel("Throttle [%]")
ax[4].set_ylabel("Brake [%]")
ax[5].set_ylabel("DRS")
ax[0].get_xaxis().set_ticklabels([])
ax[1].get_xaxis().set_ticklabels([])
ax[2].get_xaxis().set_ticklabels([])
ax[3].get_xaxis().set_ticklabels([])
ax[4].get_xaxis().set_ticklabels([])
fig.align_ylabels()
fig.legend((l1, l2), (f'{first_driver_info.familyname}', f'{second_driver_info.familyname}'), 'upper right')
plt.subplots_adjust(left=0.06 ,right=0.99, top=0.9, bottom=0.05)
# initialization function: plot the background of each frame
def init():
fig.set_data([], [], [], [], [], [], [])
return fig,
# animation function. This is called sequentially
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
fig.set_data(x, y)
return fig,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(graph1, animate, init_func=init,
frames=200, interval=20, blit=True)
# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
plt.show() |
Beta Was this translation helpful? Give feedback.
-
That too I what like a Bar Chart moving back-and-forth and all in a line one below other. |
Beta Was this translation helpful? Give feedback.
-
I've had a quick look at your code, haven't tried ever myself doing animations, but I really think that you can use an example the plotting of an histogram, with the caviats that it is 1 dimensional, don't know if it makes sense. |
Beta Was this translation helpful? Give feedback.
-
@marinik98 Let me wait for someone who knows to comment then. |
Beta Was this translation helpful? Give feedback.
-
@theOehrly Any way how to animate these telemetry graphs? |
Beta Was this translation helpful? Give feedback.
There should be a way with MatPlotLib to animate a dataframe, you can look up to https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/