.sunburst()
Published Dec 10, 2024
Contribute to Docs
The .sunburst()
method in the Plotly express
module creates sunburst charts, which are circular charts that represent hierarchical data. The chart uses sectors to represent levels in the hierarchy, with the root level at the centre and child levels radiating outward. Sunburst charts are particularly useful for visualizing nested data and their relationships.
Syntax
plotly.express.sunburst(data_frame = None, names = None, parents = None, values = None, color = None, title = None, labels = None, ...)
data_frame
: A Pandas DataFrame containing the hierarchical data to be displayed. This can also accept a dictionary or array-like objects, which are internally converted into a DataFrame.names
: The column name, Series, or array-like object that provides the labels for the sectors in the sunburst chart. This defines what each circular sector will represent.parents
: The column name, Series, or array-like object that specifies the parent-child relationships in the hierarchy. The root node will have an empty string""
as its parent.values
: The column name, Series, or array-like object used to determine the size of each sector. Larger values result in larger sectors in the chart.color
: Specifies the column name, Series, or array-like object whose values will be used to assign colors to the sectors. This is helpful for visual differentiation between groups.title
: An optional title for the sunburst chart. If not provided, no title is displayed.labels
: A dictionary that can be used to override the default labels for axis titles, legend entries, and hover information. This helps to customize the chart’s display.
Note: The ellipsis in the syntax (…) indicates that there are additional optional parameters beyond those listed here to customize the appearance and behavior of the sunburst chart.
Example
Below is an example that demonstrates how to use the .sunburst()
method with a hierarchical dataset in a Pandas DataFrame
:
import plotly.express as pximport pandas as pd# Creating a DataFrame for the sunburst chartdf = pd.DataFrame({"names": ["Company", "HR", "Sales", "Marketing", "Recruitment", "Operations", "Direct Sales", "Online Sales"],"parents": ["", "Company", "Company", "Company", "HR", "HR", "Sales", "Sales"],"values": [100, 40, 30, 30, 20, 20, 15, 15],"color": [1, 2, 3, 4, 1, 2, 3, 4]})# Creating a sunburst plot with the datafig = px.sunburst(df,names="names",parents="parents",values="values",color="color",title="Company Hierarchy and Sales Distribution")# Customizing layout and marginsfig.update_layout(margin=dict(t=50, l=25, r=25, b=25))# Displaying the sunburst plotfig.show()
The example above results in the following output:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Plotly on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours