fig, ax = plt.subplots(figsize=(14, 8))
# Compound colors and styling
compounds = {
"SOFT": {"color": "red", "alpha": 0.3},
"MEDIUM": {"color": "gold", "alpha": 0.3},
"HARD": {"color": "lightgray", "alpha": 0.3},
}
for compound, style in compounds.items():
compound_laps = clean_laps[clean_laps["Compound"] == compound]
if len(compound_laps) > 10:
# Scatter plot of raw data
ax.scatter(
compound_laps["TyreLife"],
compound_laps["FuelCorrectedTime"],
color=style["color"],
alpha=style["alpha"],
s=30,
zorder=1
)
# Group by tire life and get median
grouped = compound_laps.groupby("TyreLife")["FuelCorrectedTime"].median()
tire_life = grouped.index.values
lap_times = grouped.values
# Apply rolling median for smoothing
if len(lap_times) > 5:
smoothed_times = rolling_median(lap_times, window=5)
else:
smoothed_times = lap_times
# Plot smoothed line
ax.plot(
tire_life,
smoothed_times,
color=style["color"],
linewidth=4,
label=compound,
zorder=2
)
# Add annotation at end of line
ax.annotate(
compound,
xy=(tire_life[-1], smoothed_times[-1]),
xytext=(tire_life[-1] + 2, smoothed_times[-1]),
fontsize=14,
fontweight="bold",
color=style["color"],
va="center"
)
# Styling
ax.set_xlabel("Tire Life (Laps)", fontsize=16, fontweight="bold")
ax.set_ylabel("Fuel-Corrected Lap Time (s)", fontsize=16, fontweight="bold")
ax.set_title(
f"Tire Degradation Analysis - {session.event['EventName']} {session.event['EventDate'].year}",
fontsize=18,
fontweight="bold",
pad=20
)
# Grid and legend
ax.grid(True, alpha=0.3, linestyle="--")
ax.legend(fontsize=14, loc="upper left", framealpha=0.9)
# Set reasonable y-axis limits using quantiles
y_min = clean_laps["FuelCorrectedTime"].quantile(0.01)
y_max = clean_laps["FuelCorrectedTime"].quantile(0.99)
ax.set_ylim(y_min - 0.5, y_max + 0.5)
plt.tight_layout()
plt.show()