Member-only story
Sunburst Chart in Python
2 min readNov 26, 2024
import plotly.graph_objects as go
labels = ["Root", "Branch 1", "Branch 2", "Leaf 1", "Leaf 2", "Leaf 3"]
parents = ["", "Root", "Root", "Branch 1","Branch 1", "Branch 2"]
values = [10, 5, 5, 2, 3, 5]
fig = go.Figure(go.Sunburst(
labels=labels,
parents=parents,
values=values,
branchvalues="total", ))
fig.update_layout(
title="Sunburst Chart in Python",
margin=dict(t=30, l=0, r=0, b=0))
fig.show()
1. Importing the Library
import plotly.graph_objects as go
plotly.graph_objects
: This module from theplotly
library provides classes for creating various types of plots and charts.as go
: The aliasgo
is a shorthand to simplify the code when referencingplotly.graph_objects
.
2. Data for the Sunburst Chart
labels = ["Root", "Branch 1", "Branch 2", "Leaf 1", "Leaf 2", "Leaf 3"]
parents = ["", "Root", "Root", "Branch 1", "Branch 1", "Branch 2"]
values = [10, 5, 5, 2, 3, 5]
labels
: A list of all the nodes in the hierarchy (categories or parts of the sunburst)."Root"
is the top-level node."Branch 1"
and"Branch 2"
are second-level nodes under"Root"
."Leaf 1"
,"Leaf 2"
, and"Leaf 3"
are third-level nodes connected to…