Black Lives Matter. Please consider donating to Black Girls Code today.

Labeling percentage on each sector in sunburst chart

I made this sunburst chart:

fig =go.Figure(go.Sunburst(
    labels=["Type of Admission", "Other Medical", "Post Surgery", "Adult", "Pediatric",
            "Readmission", "Post Cath", "adult", "pediatric", "Pre-Operative"],
    parents=["","Type of Admission","Type of Admission","Post Surgery","Post Surgery",
             "Type of Admission","Type of Admission","Post Cath","Post Cath","Type of Admission"],
    values=[sum(count.values()),count['Other Medical'],(count['Adult Post Surgery']+count['Pediatric Post Surgery']),
            count['Adult Post Surgery'],count['Pediatric Post Surgery'],
            count['Readmission'],(count['Adult Post Cath']+count['Pediatric Post Cath']),
            count['Adult Post Cath'],count['Pediatric Post Cath'],count['Pre-Operative']],
    textinfo= 'label+value',
    branchvalues= "total"))

fig= fig.update_layout(title_text="Type Of Admission (2019-Q2)",margin = dict(t=0, l=0, r=0, b=0))
fig.show()

I want to label sector percentage from the total. How this could happen?Screenshot (88)

1 Like

Hello @Mahmoud_Housam, welcome to the forum! You have several possible modes for textinfo, which are Any combination of ['label', 'text', 'value', 'current path', 'percent root', 'percent entry', 'percent parent'] joined with '+' characters (I found this info by giving a mode which was not valid, the error message is actually very informative). For example:

import plotly.graph_objects as go

fig =go.Figure(go.Sunburst(
    labels=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parents=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
    textinfo='label+percent entry'
))
# Update layout for tight margin
# See https://plot.ly/python/creating-and-updating-figures/
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))
fig.show()

Hope this helps!

3 Likes

This is great @Emmanuelle !

Do you know if we have something similar for px.sunburst? to show percentage.
Otherwise I will have to reformat my data table into something go.sunburt can accept as un input which I find very complicate and there is no material here show how to do it.

Thanks

1 Like
fig = px.sunburst(....)
fig.update_traces(textinfo="label+percent")

There you go!

Thank you! you are my hero! :+1:

1 Like