I am trying to combine all of these titles into one centered in the middle, but after doing research I couldn't find the right parameter or anything.
Here is my code:
fig = px.bar(total_df, x="year", y="cases", facet_col="county_type", color="stage", text='cases', )
fig.update_traces(cliponaxis=False)
fig.update_layout(
title_text = f'Number of Cases of {specified_cancer} Cancer with County Type (2019-2020)',
yaxis_title="Total Cases",
legend_title="Stage"
)
for anno in fig['layout']['annotations']:
anno['text'] = str(anno['text']).replace('county_type=', '')
fig.show()
if anyone could help it would be awesome!
Interesting question. Have you checked the docs here: https://plotly.com/python/text-and-annotations?
Add this code to you code, replace your for loop, and see if it works:
# filter out annotations that contain the text 'year='
annotations = [anno for anno in fig['layout']['annotations'] if 'year=' not in anno['text']]
# Add centered year subtitle
annotations.append(
dict(
x=0.5,
y=1.05, # adjust if needed
showarrow=False,
text="Year",
xref="paper",
yref="paper",
font=dict(size=15),
xanchor="center",
yanchor="top"
)
)
# add the modified annotations list (which includes the original annotations, excluding ones with 'year=') and the new centered "Year" annotation.
fig['layout']['annotations'] = annotations