Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facet labels in plotly

Tags:

plotly

I'd like to change the facet labels in a plotly(_express) plot. Here's the plot:

import plotly.express as px
tips = px.data.tips()
fig = px.scatter(tips, x="total_bill", y="tip", color="smoker", facet_col="sex")
fig.show()

What I'd like is to remove the sex= from the labels.

like image 866
RoyalTS Avatar asked May 13 '26 16:05

RoyalTS


1 Answers

UPDATE: as of plotly version 4.2 in October 2019, the following usage of for_each_annotation is recommended by the docs at https://plotly.com/python/facet-plots/#customize-subplot-figure-titles

import plotly.express as px

fig = px.scatter(px.data.tips(), x="total_bill", y="tip", facet_row="sex")

fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

fig.show()

The easiest way to do this is to iterate over the annotations:

for a in fig.layout.annotations:
    a.text = a.text.split("=")[1]
like image 163
nicolaskruchten Avatar answered May 16 '26 02:05

nicolaskruchten