Let the Sunburst! — The complete Guide On How To Plot Sunburst Charts in Plotly

A beginner to advanced guide to sunburst charts in Plotly

Arslan Shahid
Towards AI

--

Image by the Author

After three years of working in data-related roles, I can safely say that the most underrated skill is good data visualization. Having a toolbox of visualization kits available at your disposal is crucial. Plotly has issues but overall, it can help you design very intricate and aesthetically amazing visualizations. One of the most underappreciated visuals in the Plotly library is sunburst charts.

This tutorial step by step goes over these topics, with each gradually becoming more advanced:

  1. What are sunburst charts and use cases for when to use sunburst charts
  2. Basics of plotting sunburst through Plotly express
  3. Plotting as a Plotly graph object
  4. Advanced styling and best practices

Without further ado, let's start

What are sunburst charts & how are they used?

As Elon Musk once remarked, telling why something is useful is a very good motivator to make people learn. So sunburst charts, are a data visualization based on overlaying circles which can tell about how different categories or groups relate to each other. Below are some use cases for which I have personally used sunburst charts:

  1. Customer Segmentation: At one of my jobs I was required to show how different business-defined customer segments relate to each other. My goto tool was a sunburst chart
  2. Conversion Metrics: Many of you would have heard of CPC, Click-through, and CAC ratios, etc. One way to show how different buckets of these metrics relate to each other is through using a sunburst chart.
  3. Correlation of price movements: In a previous post I showed how much % change in BTC, corresponds to price movement in ETH. It was a simple, intuitive way to tell how they are approximately correlated.

I hope you understand why they are a useful visualization, now let’s get to the fun part.

Plotting sunbursts using Plotly express.

It is always better to begin with the basics. Plotly library has two functioning components, one Plotly express and the other graph objects. Plotly Express is designed for a plug-and-play style of data visualization, however, most return types are Plotly graph objects. If you have a data frame it is recommended you use plotly express for quick visuals.

Below is the simple code for a sample data frame using Plotly Express. You can download this dataset here. If you’re a beginner, I highly suggest you use the sample dataset.

import plotly.express as px
import pandas as pd
import numpy as np

#Reading sample dataframe, make sure the data is in the same directory as the python file
# this data set is randomly generated, not actual data
df = pd.read_csv('sample_data.csv')

#The sample dataset contains generated data about data-roles related pay.

#grouped contains mean pay per Job-title & biological sex
grouped = df.groupby(['Job','Sex']).agg({'Pay':'mean'})

#optional house-keeping step, changing column names
grouped.columns = ['Job','Sex','Average_Pay']

#Plotly doesn't display numbers as rounded, so preferable to do that
# For more detailed styling please visit the Advanced styling portion

grouped['Average_pay'] = [int(x) for x in grouped['Average_pay']]
# fig is commonly used as convention in plotly figures
fig = px.sunburst(grouped, path=['Job', 'Sex','Average_pay'], values ='Average_pay', title ='Average Pay of Data roles by biological sex')
#fig.show() displays the figure
fig.show()
Image generated by above line of code — snapshot by Author

This shows the basic way you can plot sunburst charts. The next portion would be about constructing a sunburst chart using graph_object in Plotly.

Making sunburst charts as Plotly graph objects

The reason to use Plotly graph objects is usually when you are not able to create the desired sunburst chart using Plotly Express. In my experience, doing it through graph objects also makes integrating different visualizations into a single visual. The third benefit I see is that graph objects might be better able to handle unstructured data, if you don’t have a data frame to work with it might make more sense to use graph_object.

Note: As mentioned previously whether you use Plotly Express or graph objects. The return type of a figure will always be the same, a graph object.

Before we dive into the code Plotly graph objects have three main arguments:

  1. Labels — Contains information about the assigned labels of each block in a sunburst chart.
  2. Parents — Tells for each label which is the parent of each label. For every block in the sunburst, the list tells which block is its parent. No parent is denoted as “”
  3. Values — tells the relative width of each block.

Below is a detailed illustration that is helpful. It depicts how plotly reads these three lists. I would highly suggest playing around with below code, changing these three lists and trying to predict how the figure changes.

Image by the Author depicts how Plotly graph object visualizes a sunburst chart. Could be used under CC license by linking this article.
import plotly.graph_objects as go

#The three neccessary lists
# labels tells all of the blocks in the chart
labels_list = ['Label 1', 'Label 2', 'Label 3', 'Label 4','Label 5','Label 6']
# Parent tells in which order these labels will appear
parents_list = ["",'Label 1','Label 1','Label 3','Label 4','Label 3']
# values tells the relative size give to each of these labels
values_list = [2,1,2,3,2,4]

#How to plot this syntax
fig = go.Figure(go.Sunburst(
labels = labels_list,
parents = parents_list,
values = values_list,

)
)
# Adds title
fig.update_layout(title = 'Sunburst')

#Display
fig.show()
Image by the Author. shows the output of the above code

Advanced Styling

Recall that whether you use Plotly express or graph objects when you have plotted the initial figure you can style them the same way. Styling is based on personal preferences, I will show how I prefer to style them. In this tutorial, I will be using the figure drawn in the plotly express section and making some meaningful stylistic changes.

Note: Some styling is the same across different charts. If you want a detailed explainer on how to quickly make overall stylistic changes to Plotly figures, please do consider giving the below explainer a read.

Styling any Plottly figure has two different components: one data and one layout.

Data: Include things specific to the data of the chart, like in the case of sunburst charts things like block size, block color, labels, and hovertemplate.

Layouts: include things like background color, title, margins, and legends.

Editing Data

Image by the Author — fig.data[0] (left) and figure on right

Pro Tip: Pay special attention to how in the data the ‘ids’ variable looks. If you want to edit all the children of ‘Research Scientist’ you can loop over all the ids that contain ‘Research Scientist’. If you want to edit the ‘Female’ branch of ‘Research Scientist’ you can reference using ‘Research Scientist/Female’ and so on.

For this exercise, our business goal is to highlight the lowest-paid male profession and highest-paid male profession.

# This code fixes the labels

#Ads spacing in big words to be taken to another line -- Plotly recognises some html tags
fig.data[0]['labels'] = [x.replace(' ','<br>') if x.isnumeric()==False else x for x in fig.data[0]['labels']]

#Converts numeric numbers into '0,.0K' format
fig.data[0]['labels'] = [format(int(x)/1000,'0,.1f')+'K' if x.isnumeric()==True else x for x in fig.data[0]['labels']]
Image by the Author — Shows how to fix labels
i = 0
ids_dict = {}

for x in fig.data[0]['ids']:
ids_dict[x] = i
i+=1



#you cannot directly assign 1 value for anything in fig.data[0], so creating a temp where you make changes is recommended
temp = fig.data[0]['marker']['colors']

#Assigning red color to Data Analyst Male
temp = ['Red' if 'Data Analyst/Male' in x or x=='Data Analyst' else '#5A5A5A' for x in fig.data[0]['ids']]


#Assigning green color to Research Scientist Male
temp[ids_dict['Research Scientist/Male']] = '#008000'

temp[ids_dict['Research Scientist/Male/55699']] = '#008000'

temp[ids_dict['Research Scientist']] = '#008000'

fig.data[0]['marker']['colors'] = temp
Image by the Author — Shows the highest-paid male profession and lowest-paid male profession. Coloring choices can be changed based on business goals.

Editing Layout

Image by the Author, showing all layout options to edit for sunburst charts

For this tutorial, I will only be showing a few of these options but you should explore more options by searching the functionality on Plotly documentation.

#update function for layout
fig.update_layout(

#Ads a background color to plot area
plot_bgcolor = '#f6f6f6',
#Ads a background to the whole paper
paper_bgcolor ='#f6f6f6',
#Changes the font setting, you might need to download some fonts to use them
font = dict(family='Arsenal',size =17)
#Englarges the font area by reducing the margin between main plot and other sections
,margin=dict(t=40, l=20, r=20, b=20),
#Changes the title, you can use Html tags to add a subtitle using <sup>
title = '<b>Average Pay of Data Roles by Biological Sex</b><br><sup>Highest & Lowest Paid Male Professions</sup>')
Image by the Author — Shows the final image.

This concludes this tutorial, please do check out some of my other writings.

If you want to hire me directly for any data-related task please do reach out here: https://topmate.io/arslan_shahid

Thank you for reading!

--

--

Life has the Markov property, the future is independent of the past, given the present