facet labels in plotly

徘徊边缘 提交于 2021-02-08 10:40:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!