# Sunburst charts in plotly

## Sunburst plots in plotly with px.sunburst

Sunburst charts are very similar to treemaps and are used to visualize hierarchical data by displaying concentric rings at different levels of hierarchy. The function to create sunburst plots in plotly express is named sunburst and you will need to input the categories to names, its corresponding parent to parents and the associated value to each group to values.

import plotly.express as px

data = dict(
categories = ['A', 'B', 'C',
'A1', 'A2', 'A3',
'B1', 'B2',
'C1', 'C2', 'C3'],
parent = [ '', '', '', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'],
value = [35, 25, 40, 10, 15, 10, 12, 13, 10, 20, 10])

fig = px.sunburst(data,
names = 'categories',
parents = 'parent',
values = 'value')

fig.show()

Click on a parent category (A, B or C in the previous example) to see a cool animation that will display the leafs only for that category. Click again to come back to the default plot.

Branch values

You might have noticed that despite the sum of the groups at the second level is equal to the sum of values of the first level groups they don’t fill the whole area. This is because by default the branchvalues argument is set to 'remainder'. When set to 'total', the sum of values is based on their parents, while when set to 'remainder' the values are taken as an extra part.

import plotly.express as px

data = dict(
categories = ['A', 'B', 'C',
'A1', 'A2', 'A3',
'B1', 'B2',
'C1', 'C2', 'C3'],
parent = [ '', '', '', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'],
value = [35, 25, 40, 10, 15, 10, 12, 13, 10, 20, 10])

fig = px.sunburst(data,
names = 'categories',
parents = 'parent',
values = 'value',
branchvalues = 'total')

fig.show()

Center group

Note that the first level of the sunburst can be a central group containing the rest. For this purpose you will need to create a new category that won’t have any parent. This category must be the only one without parent.

import plotly.express as px

data = dict(
categories = ['Total', 'A', 'B', 'C',
'A1', 'A2', 'A3',
'B1', 'B2',
'C1', 'C2', 'C3'],
parent = ['', 'Total', 'Total', 'Total', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'],
value = [100, 35, 25, 40, 10, 15, 10, 12, 13, 10, 20, 10])

fig = px.sunburst(data,
names = 'categories',
parents = 'parent',
values = 'value',
branchvalues = 'total')

fig.show()

Color customization

There are several ways to customize the color of a sunburst. By default, the colors are assigned based on the categories of the first ring. However, you can pass a categorical or numerical variable to color, so the sunburst will be colored based on that variable with a discrete or continuous palette.

You can also customize the color palette, passing an array of colors to color_discrete_sequence or passing a dict to color_discrete_map. Note that you can pass a single color to the array, so a color palette will be used based on that color.

import plotly.express as px

data = dict(
categories = ['Total', 'A', 'B', 'C',
'A1', 'A2', 'A3',
'B1', 'B2',
'C1', 'C2', 'C3'],
parent = ['', 'Total', 'Total', 'Total', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'],
value = [100, 35, 25, 40, 10, 15, 10, 12, 13, 10, 20, 10])

fig = px.sunburst(data,
names = 'categories',
parents = 'parent',
values = 'value',
color_discrete_sequence = ['orange'],
branchvalues = 'total')

fig.show()

Plotly express charts are very easy to customize with traces. In this case there are sunburst traces that can be used to customize advanced settings. In the following example we set a minimum font size of 20 and if the label doesn’t fit it will be hidden.

import plotly.express as px

data = dict(
categories = ['Total', 'A', 'B', 'C',
'A1', 'A2', 'A3',
'B1', 'B2',
'C1', 'C2', 'C3'],
parent = ['', 'Total', 'Total', 'Total', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'],
value = [100, 35, 25, 40, 10, 15, 10, 12, 13, 10, 20, 10])

fig = px.sunburst(data,
names = 'categories',
parents = 'parent',
values = 'value',
branchvalues = 'total')
fig.update_layout(uniformtext = dict(minsize = 20, mode = 'hide'))

fig.show()

## Sunburst plot from a rectangular data frame

If you have a rectangular data frame, you can use it to create your sunburst without the need of specifying the names and their parents. In this scenario you will need to use the path argument and specify the desired column names of the variables to be used.

import plotly.express as px

# Sample data
df = px.data.tips()

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

fig.show()

Color based on a categorical variable

Recall that you can set the color for each slice based on a categorical variable. In the following example we set the day column as the variable to be used to set the colors.

import plotly.express as px

# Sample data
df = px.data.tips()

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

fig.show()

Color based on a continuous variable

You can also set the color based on a numerical variable. For instance, in the following example we are setting the tip column and now we can observe, for instance, that the females of the sample give more tips on Sunday at dinner.

import plotly.express as px

# Sample data
df = px.data.tips()

fig = px.sunburst(df,
path = ['sex', 'day', 'time'],
color = 'tip')

fig.show()

A practical introduction