Visualize race pace patterns across all drivers and laps
A lap time heatmap shows race pace across all drivers and laps. Color coding identifies consistent performers, tire degradation patterns, pit stop strategies, and incidents.
Reshape the data so drivers are rows and lap numbers are columns.
# Create pivot tableheatmap_data = laps_clean.pivot_table( index='Driver', columns='LapNumber', values='LapTimeSeconds', aggfunc='first')# Sort drivers by average lap time (fastest at top)driver_avg = heatmap_data.mean(axis=1).sort_values()heatmap_data = heatmap_data.loc[driver_avg.index]
Use the seaborn heatmap function to draw the chart.
fig, ax = plt.subplots(figsize=(16, 10))sns.heatmap( heatmap_data, cmap='RdYlGn_r', # Red (slow) to Green (fast) vmin=fastest_lap, vmax=fastest_lap * 1.07, cbar_kws={'label': 'Lap Time (seconds)', 'aspect': 40}, linewidths=0.5, linecolor='#1a1a1a', xticklabels=5, # Show every 5th lap number ax=ax)# Stylingax.set_xlabel('Lap Number', fontsize=12, fontweight='bold')ax.set_ylabel('Driver', fontsize=12, fontweight='bold')ax.set_title('Monaco Grand Prix - Lap Time Heatmap', fontsize=14, fontweight='bold', pad=20)plt.yticks(rotation=0, fontsize=10)plt.xticks(fontsize=10)plt.tight_layout()plt.show()
The native function exposes the same filters as parameters. laptime_cutoff fixes the color scale, from fastest to fastest * cutoff. include_deleted and include_pit_laps keep those laps when True. cmap and xticklabels control the styling:
import tif1# Defaults match the chart abovefig, ax = tif1.plot_laptime_heatmap(2023, 'Monaco Grand Prix', 'R')plt.show()# Custom color scale and tick spacingfig, ax = tif1.plot_laptime_heatmap( 2023, 'Monaco Grand Prix', 'R', cmap='viridis', xticklabels=3, laptime_cutoff=1.10,)plt.show()
The whole workflow above is wrapped in a single native function:
import tif1import matplotlib.pyplot as plt# One call: loads the race, pivots the data, and draws the heatmapfig, ax = tif1.plot_laptime_heatmap(2023, 'Monaco Grand Prix', 'R')plt.show()# Or save straight to a file# tif1.plot_laptime_heatmap(2023, 'Monaco Grand Prix', 'R', save_path="laptime_heatmap.png")
Focus on a subset of drivers for detailed comparison:
# Select the 5 fastest drivers by average lap timetop_drivers = driver_avg.index[:5]laps_top = laps_clean[laps_clean['Driver'].isin(top_drivers)]heatmap_top = laps_top.pivot_table( index='Driver', columns='LapNumber', values='LapTimeSeconds', aggfunc='first')# Plot with larger cells for better visibilityfig, ax = plt.subplots(figsize=(16, 6))sns.heatmap(heatmap_top, cmap='RdYlGn_r', linewidths=1, ax=ax)ax.set_title('Top 5 Finishers - Lap Time Comparison')plt.tight_layout()plt.show()