I’ve got a plot in a Dash app where the Y-axis comes from a Pandas Categorical series. By default only the labels that are in the data currently being plotted are being shown; I would like the axis to show all of the categorical values defined in the series, not just the ones present in the currently filtered data.
I’ve come up with this but it feels a bit convoluted. Wondering if there’s a better way, or, if not maybe someone trying to do something similar will find this!
# df has a column with a categorical series
categories = df[column].cat.categories
category_indices = [i for i, _ in enumerate(categories)]
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df[x_column],
y=df[column].cat.codes,
name=column,
),
)
fig.update_yaxes(
tickvals=category_indices,
ticktext=list(categories),
tickmode="array",
range=[category_indices[0] - 0.1, category_indices[-1] + 0.1],
# If it's categorical it goes on order that data appears which may produce incorrect chart!
type="linear"
)
fig.show()