How to Create a Sunburst Chart in Plotly Python?

Spread the love

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
url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/MPVDataset.csv'
df = pd.read_csv(url)
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()

Adding more branches –

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 –

  1. How to install plotly python with pip?
  2. How to create a Line Chart with Plotly Python?
  3. How to create Scatter plot in Plotly Python?
  4. How to create a Bar Chart in Plotly Python?
  5. How to create Horizontal Bar Chart in Plotly Python?
  6. How to create a Histogram in plotly python?
  7. How to Create a Box Plot in Plotly Python?
  8. How to create a Pie Chart in Plotly Python?
  9. How to create a Dot Plot in Plotly Python?
  10. How to Create Heatmap with Plotly Python?
  11. How to Create a Violin Plot in Plotly Python?
  12. How to Create Subplots in Plotly Python?
  13. How to Create a Bubble Chart in Plotly Python?
  14. How to Create a Gantt Chart in Plotly Python?
  15. How to Create an Area Chart in Plotly Python?
  16. How to Create Tables in Plotly Python?

Rating: 1 out of 5.

Leave a Reply