I want to keep the current tick layout on my plotly express chart, but instead of having the raw values, have labels. Basically, I like the fact there’s only a few ‘main’ ticks and not a tick for every value, but I need to change the text on the labels.
I have been trying to figure this out using the ‘Array’ tickmode and sorting, but this either sorts the chart (which I don’t want), or the labels generated are unevenly spaced.
Here is my original code before I messed it up!
def lap_times_graph(year, d_ids, laps_df, races_df, col):
for d_id in d_ids:
times = {"x": [], "y": [], "Name": [], "Lap": [], "Time": []}
# Obtains each race the selected driver was in for the selected year
races_df = races_df.loc[races_df["year"] == year, ["race_id", "name"]]
for index, r in races_df.iterrows():
df_laps = laps_df.loc[(laps_df["race_id"] == r["race_id"]) & (laps_df["driver_id"] == d_id), ["lap", "time", "milliseconds"]]
df_laps2 = df_laps.sort_values(by="milliseconds")
for index, r1 in df_laps.iterrows():
times["x"].append(r1["milliseconds"])
times["y"].append(r["race_id"])
times["Name"].append(r["name"])
times["Lap"].append(r1["lap"])
times["Time"].append(r1["time"])
df = pd.DataFrame(data=times)
# Remove outliers
z = np.abs(stats.zscore(df['x']))
threshold = 3
outliers = df[z > threshold]
df = df.drop(outliers.index)
fig = px.strip(df, x="x", y="y",title="Lap Times<br><sub class='subtitle'>Hover over points for more details</sub>",
labels = {"x": "Lap time", "y": "Race"},
color_discrete_sequence = n_colors("rgb(255, 75, 75)", "rgb(255,255,255)", 3, colortype="rgb"), color="y",
hover_name="Name", hover_data={"x":False, "y":False, "Lap":True, "Time":True})
fig.update_layout(
showlegend = False,
yaxis = dict(
side = "right",
showticklabels = False),
xaxis = dict(
tickangle = -45)
)
col.plotly_chart(fig, use_container_width=False)
Is there any way to change the labels on the default ticks? r1["Time"]
Holds values with the correct format I’m looking for
An example of what I’m looking to achieve: (changes in yellow)
Thanks..