Reputation: 137
I am working with pandas and plotly to map out f1 data. I currently have been able to retrieve the following data:
driverId position time lap
0 bottas 3 0 days 00:01:46.833000 1
0 bottas 3 0 days 00:01:40.188000 2
0 hamilton 1 0 days 00:01:44.859000 1
0 hamilton 1 0 days 00:01:39.625000 2
0 max_verstappen 2 0 days 00:01:46.339000 1
0 max_verstappen 2 0 days 00:01:39.613000 2
0 perez 4 0 days 00:01:48.257000 1
0 perez 4 0 days 00:01:40.612000 2
I have been able to plot the data successfully, and would like to apply markers to only selected driverId's e.g. just perez and bottas
To generate the lap vs time plot:
fig = px.line(
df,
x="lap",
y="time",
color="driverId",
color_discrete_map=colour_drivers,
hover_name=df["time"].astype("int64").apply(strfdelta),
hover_data={"time": False, "position": True},
)
I have tried to use fig.update_traces(mode="markers+lines")
but that applies markers to all drivers. I have also tried creating a fig.add_scatter
objects but, I do not want to abandon the line plot as it is more efficient for a larger number of drivers, and would introduce for-loops which I want to avoid. How would I go about applying it to a select group of drivers of my choosing, preferrably using plotly.express.line
?
Upvotes: 0
Views: 409
Reputation: 31146
colour_drivers
px.scatter
and add_traces()
to line plot. Drivers are filtered in dataframeimport plotly
colour_drivers = {d:c for c,d in zip(plotly.colors.qualitative.Alphabet, df["driverId"].unique())}
fig = px.line(
df,
x="lap",
y="time",
color="driverId",
color_discrete_map=colour_drivers,
hover_name=df["time"].astype("int64").apply(strfdelta),
hover_data={"time": False, "position": True},
)
fig.add_traces(px.scatter(df.loc[df["driverId"].isin(["bottas","perez"])], x="lap", y="time",
color="driverId", color_discrete_map=colour_drivers).update_traces(showlegend=False).data)
Upvotes: 1