'Plotly Sunburst chart with percentages
I would like to create a Sunburst chart, corresponding to the following:
Where:
- B is 70% of A
- C is 20% of B
- D is 50% of B
I looked at the plotly documentation but the examples provided only used either use a dataset or a values argument which I don't understand how it works.
Do someone have an example I can rely on, that is similar to what I'm looking for?
Solution 1:[1]
There is a setting that allows percentages to be added to labels, which would make this possible. fig.update_traces(textinfo="label+percent parent") The answer is a modified code based on the example in the official reference.
import plotly.express as px
data = dict(
character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
value=[10, 14, 12, 10, 2, 6, 6, 4, 4])
fig = px.sunburst(
data,
names='character',
parents='parent',
values='value',
)
fig.update_traces(textinfo="label+percent parent")
fig.update_layout(
autosize=False,
height=500,
width=500
)
fig.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | r-beginners |


