plotly

Plotly.Express.Sunburst

“Sunbursts are a type of plot that uses radial axes to visualize hierarchical data spanning from the root level. Sunbursts are closely similar to Treemap plots or icicle plots.

The data starts at the center, and all the children span the outside rings of the plot.

In this article, we will explore how we can create sunburst plots using the Plotly Express module.”

Function Syntax and Parameter List

The function syntax is expressed below:

plotly.express.sunburst(data_frame=None, names=None, values=None, parents=None,

path=None, ids=None, color=None, color_continuous_scale=None, range_color=None,

color_continuous_midpoint=None, color_discrete_sequence=None,

color_discrete_map=None, hover_name=None, hover_data=None, custom_data=None,

labels=None, title=None, template=None, width=None, height=None,

branchvalues=None, maxdepth=None)

The most useful parameters when using this function are as shown below:

  1. data_frame – defines the data frame containing the column used in the plot.
  2. names – specifies the values used as labels for the sectors.
  3. values – defines the values used to set the values associated with the sectors.
  4. parents – defines the values used as parents in the sunburst.
  5. path – specifies the values used to define the hierarchy of sectors from the root level.
  6. ids – sets the values used to set the ids of the sectors.

Example

The code below illustrates how to create a simple sunburst plot.

import plotly.express as px
data = dict(
    distros=['Debian', 'CentOS', 'Fedora', 'Red Hat Linux', 'OpenSUSE', 'Qubes', 'SUSE Studio', 'Ubuntu', 'Kubuntu', 'Xubuntu'],
    parents=['', 'Red Hat Linux', 'Red Hat Linux', '', 'Red Hat Linux', 'Fedora', 'OpenSUSE', 'Debian', 'Ubuntu', 'Ubuntu']
)
fig = px.sunburst(data, names='distros', parents='parents')
fig.show()

The figure above should return a sunburst figure as shown:

We can create a distinct color for each sector by setting the color parameter as shown:

import plotly.express as px
data = dict(
    distros=['Debian', 'CentOS', 'Fedora', 'Red Hat Linux', 'OpenSUSE', 'Qubes', 'SUSE Studio', 'Ubuntu', 'Kubuntu', 'Xubuntu'],
    parents=['', 'Red Hat Linux', 'Red Hat Linux', '', 'Red Hat Linux', 'Fedora', 'OpenSUSE', 'Debian', 'Ubuntu', 'Ubuntu']
)
fig = px.sunburst(data, names='distros', parents='parents', color='distros')
fig.show()

The code above should assign a unique color based on the distribution as shown:

Closing

This article covers the basics of creating sunburst plots using the plotly express module.

Happy coding!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list