# How to Create a Sunburst Chart in Plotly Python?

In this post you will learn how to create a sunburst chart in Plotly Python.

## Sunburst Chart –

Sunburst plots visualize hierarchical data spanning outwards radially from root to leaves. The root starts from the center and children are added to the outer rings.

Let’s read a dataset to illustrate it.

import pandas as pd
df = df[df["State"].isin(['AL', 'OR', 'SC'])]
df = df[df["Victim's race"].isin(["White", "Black", "Hispanic", "Asian"])]
df.head()

### Create a Sunburst Chart with Plotly Express –

Now let’s create a sunburst chart using the px.sunburst. The 1st level in a sunburst chart will the the root, the second level will be the branches and the last level in the sunburst chart will the the leaves. We can have more than one branches.

Let’s create a sunburst chart where the root will be the state, branch will be the unarmed column and the leaves will be the victim’s gender.

import plotly.express as px
fig = px.sunburst(
data_frame=df,
path=["State", "Unarmed", "Victim's gender"]
)
fig.show()

Let’s say you also want to add the victim’s race column in the sunburst chart.

import plotly.express as px

fig = px.sunburst(
data_frame=df,
path=["State", "Unarmed","Victim's race", "Victim's gender"]
)
fig.show()

## Changing the Color –

You can also change the color of the sunburst chart based on any columns. Let’s set the color parameter to victim’s race.

import plotly.express as px
fig = px.sunburst(
data_frame=df,
path=["State", "Unarmed","Victim's race"],
color = "Victim's race"
)
fig.show()

### Branchvalues –

You can also use the branchvalues parameter. By default it is set to total. We can change it to remainder to create a different type of sunburst chart.

import plotly.express as px
fig = px.sunburst(
data_frame=df,
path=["State", "Unarmed","Victim's race"],
color = "Victim's race",
branchvalues='remainder'
)
fig.show()

### Adding a Title to the Sunburst Chart –

You can also add a title to the sunburst chart using the title parameter.

import plotly.express as px
fig = px.sunburst(
data_frame=df,
path=["State", "Unarmed","Victim's race"],
color = "Victim's race",
title="Breakdown of deaths by police"
)
fig.show()

### Related Posts –

Rating: 1 out of 5.