I seem to have an issue with a figure with subplots.
I have a script that generates traces, assigns them to subplots and then generates the graph with make_subplot.
It generates a graph like the following:
I would like to have the x axis labels (with time) repeated below each subplot.
I therefore followed the advice in Subplots with shared x-axes BUT show x-axis for each plot - #2 by empet and ensured that I have showticklabels=True
on every xaxis.
Yet, nothing works, and the tick labels still only appear on the bottom subplot.
Here is a (shortened) version of the code:
@dataclass
class SubplotInfo:
traces: list[go.Bar]
categories: list[str]
title: str
def _create_figure_with_subplots(subplots: list[SubplotInfo]):
"""Create a figure with subplots from a list of subplots"""
fig = make_subplots(
rows=len(subplots),
cols=1,
shared_xaxes=True,
subplot_titles=[s.title for s in subplots],
vertical_spacing=0.1,
)
for i, subplot in enumerate(subplots):
for trace in subplot.traces:
fig.add_trace(trace, row=i + 1, col=1)
# Set y-axis labels
fig.update_yaxes(
tickvals=subplot.categories,
ticktext=subplot.categories,
title_text="",
autorange="reversed",
row=i + 1,
col=1,
)
# Set x-axis as date/time
fig.update_xaxes(
type="date",
title_text="",
showticklabels=True,
showspikes=True,
spikemode="across",
showline=True,
spikesnap="cursor",
spikedash="solid",
spikethickness=1,
spikecolor="gray",
rangeslider=dict(visible=(i == len(subplots) - 1), thickness=0.05),
row=i + 1,
col=1,
)
# once more, for good measure...
fig.update_xaxes(showticklabels=True)
# Set layout
fig.update_layout(
barmode="stack",
legend=dict(groupclick="toggleitem"),
hovermode="closest",
hoversubplots="axis"
)
# Force a single xaxis to allow spike lines to span all subplots
fig.update_traces(xaxis=f"x{len(subplots)}")
return fig
What I donβt understand is that when I examine fig
on the last line, I can clearly see that xaxis1, xaxis2 and xaxis3 all have showticklabels
set to Trueβ¦