Polar
Polar

Reputation: 147

Plotly Facet_col subplots titles

I read many posts similar to my questions but for some reasons they still don't work. I'm working on the well known plotly database 'tips'. I want to show 2 subplots by 'sex' and then edit both subplots' titles. I start from this code:

df=px.data.tips()
names={'Femmine':'Plot1','Maschi':'Plot2'}
fig=px.box(df,x='day',y='tip',facet_col='sex')

Now I'd like to edit the default subtitles outcome('sex=Female', 'sex=Male') and replace them with two names i stored in the dictioanry names. I tried this:

fig.for_each_annotation(lambda a: a.update(text=str(names.keys())))
fig 

But the output gives me this:

enter image description here

Really, i know the solution is there but ...I simply can't find it. Any help? Thanks

Upvotes: 0

Views: 1114

Answers (1)

PythonicQuestions
PythonicQuestions

Reputation: 21

Based on the answer here: How to change titles (facet_col )in imshow (plotly)

df=px.data.tips()
names={'Femmine':'Plot1','Maschi':'Plot2'}
fig=px.box(df,x='day',y='tip',facet_col='sex')

for i, label in enumerate(names):
    fig.layout.annotations[i]['text'] = label

fig.show()

Note that this works because your dictionary keys are the title you wish to update.

Otherwise, if you wanted the value instead (Plot1, Plot2), you should instead use:

for i, (key, value) in enumerate(names.items()):
    fig.layout.annotations[i]['text'] = value

Upvotes: 2

Related Questions