问题
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.
回答1:
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]
来源:https://stackoverflow.com/questions/57265944/facet-labels-in-plotly