plotly Sunburst Chart in Python (4 Examples)


Hi! This tutorial will show you how to build a plotly sunburst chart in the Python programming language.

A sunburst chart is used to display hierarchical data, with each level of the hierarchy represented by a circle or an arc that is sliced for each category.

Sunburst charts require the tree data model, where the “id” is used to set unique identifiers; “parent” is used to set the parent node; “children” is used to setting the children nodes; and “values” is used to set the chart values.

Just as in a biological context, the children nodes emerge from the parent node, and the children can also have their own children, which can be mapped in the chart.

Here is a quick overview:

Let’s jump right into the Python code!


Install & Import plotly & pandas

In order to create a sunburst chart, we will need to first of all, install and import plotly. Therefore, if you do not have plotly already installed in your Python programming environment, run the lines of code below in your Python IDE to install and import it; otherwise, you may skip to the next section in this tutorial:

# install plotly
pip install plotly
# import plotly
import as px

We will also need to install and import pandas as well, which is the foremost library in Python for data analysis and manipulation. To install and import pandas, run the lines of code below:

# install pandas
pip install pandas
# import pandas
import pandas as pd

With plotly and pandas installed and imported, we can now build sunburst charts.

Import Sample Dataset

In this tutorial, we will make use of the popular tips dataset, which comes preloaded in the Python plotly library. To load the dataset, run the following line of code:

df =

You can take a look at the first five rows of the dataset:

#  total_bill	 tip	 sex	smoker	day	time	size
#0	16.99	1.01	Female	No	Sun	Dinner	2
#1	10.34	1.66	Male	No	Sun	Dinner	3
#2	21.01	3.50	Male	No	Sun	Dinner	3
#3	23.68	3.31	Male	No	Sun	Dinner	2
#4	24.59	3.61	Female	No	Sun	Dinner	4

This is the sample dataset that we will use for the sunburst chart examples in this tutorial.

Example 1: Basic Sunburst Chart

In this first example, we will build a basic sunburst chart. Therefore, run the lines of code below to build the basic sunburst chart:

fig = px.sunburst(df, path = ["day", "time", "sex"],values = "total_bill")

In the example above, the code with which we created the sunburst chart is very simple and straightforward. In the px.sunburst() function, we indicated what value we wanted to plot, which is “total bill” as well as the hierarchical order to be mapped, which is from day to time and time to sex.

Example 2: Sunburst Chart with Discrete Color

In this next example, we will make a slight change to the order of the data and will parse a discrete color to the function. Run the lines of code below:

fig = px.sunburst(df,path = ["sex", "day", "time"],values = "total_bill", color = "day")

In this example, we altered the hierarchy of the data by making “sex” the root node, and then we colored the sectors of the sunburst chart using “day” as the discrete color value in the px.sunburst() function, where we parsed “day” to the color = argument.

Example 3: Sunburst Chart with Explicit Color Mapping

In this third example, we will build a sunburst chart and explicitly map its discrete colors:

fig = px.sunburst(df,
                  path = ["sex" ,"day", "time"],
                  values = "total_bill",
                  color = "time",
                  color_discrete_map = {"(?)":"green","Lunch":"red","Dinner":"darkblue"})

In the above code, we changed the hierarchical order and parsed a dictionary of discrete colors to the color_discrete_map = argument. In that dictionary, we gave the first key as (?) to indicate the root node along with its color value. Then we demarcated lunch and dinner by giving them separate colors.


Example 4: Sunburst Chart with New Text Orientation

For this example, we will need to import plotly graph objects, and we will also make use of the coffee flavors dataset.

In order to load the coffee flavors dataset, we will need to, first of all, install and import the Python pandas library:

# install pandas
pip install pandas
# import pandas
import pandas as pd

With pandas installed and loaded, we can now import the coffee flavors dataset:

df = pd.read_csv('')

Now that the dataset has been loaded, we can now plot the sunburst chart.

import plotly.graph_objects as go
# load dataset
fig = go.Figure()
    ids = df.ids,
    labels = df.labels,
    parents = df.parents,
    domain = dict(column = 1),
    maxdepth = 2,
    insidetextorientation = "radial"
    margin = dict(t = 10, l = 10, r = 10, b = 10)

In the code above, we first of all initialized the go figure by running fig = go.Figure(), then we added trace to the figure, and within the trace, we defined the argument insidetextorientation = as “radial”. We can also parse “tangential” to this argument, which is another text orientation option.

Lastly, we updated the chart layout using the update_layout() function in which we parsed a dictionary of the top, left, right, and bottom margins of the plot.

Video, Further Resources & Summary

Do you need more explanations on how to build plotly sunburst charts in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to build plotly sunburst charts in Python.


The YouTube video will be added soon.


With that, we have demonstrated how to make a plotly sunburst chart in the Python programming language. As you can see, it is another interesting way to interactively visualize hierarchical data in Python.

This post has shown how to build plotly sunburst charts in Python. In case you have further questions, you may leave a comment below.


R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.