Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing facet headings on a Plotly chart

Tags:

pandas

plotly

So I want to customise my chart axes titles

So my graphs are printing out the correct data but for one of the graphs the axis does not match the hover text.

df["M "] = df["Date_Modified"]
fig = px.bar(df, x = "M ", y = "Count", facet_row = "Entity",     color = "Entity") 
fig.update_traces(marker_line_width=0, opacity=1)
fig.update_layout(title=(option.title() + " by company"), yaxis_zeroline=False, xaxis_zeroline=False)
fig.update_xaxes(title_text = "")
    fig.show()

enter image description here

So I want to get rid of the entity axis and also make the count be only displayed once

enter image description here

I want the Facet title for the second image be more clean so just Feb19 or Mar 19 and not with an = sign

like image 792
ritzrori Avatar asked Mar 18 '26 05:03

ritzrori


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 facet titles here are stored as annotations in the figure, under fig.layout.annotations so you can edit them directly in there with e.g. fig.layout.annotations[0].text = "new text"

Here's a simple loop to just keep the part after the = sign:

import plotly.express as px

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

for annotation in fig.layout.annotations:
    annotation.text = annotation.text.split("=")[1]

fig.show()
like image 66
nicolaskruchten Avatar answered Mar 20 '26 13:03

nicolaskruchten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!