Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!
Note: Sunburst Charts are available in version 3.8+
Run pip install plotly --upgrade
to update your Plotly version
import plotly
plotly.__version__
'3.10.0'
import plotly.plotly as py
import plotly.graph_objs as go
trace = go.Sunburst(
labels=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
outsidetextfont = {"size": 20, "color": "#377eb8"},
marker = {"line": {"width": 2}},
)
layout = go.Layout(
margin = go.layout.Margin(t=0, l=0, r=0, b=0)
)
py.iplot(go.Figure([trace], layout), filename='basic_sunburst_chart')
With branchvalues "total", the value of the parent represents the width of its wedge. In the example below, "Enoch" is 4 and "Awan" is 6 and so Enoch's width is 4/6ths of Awans. With branchvalues "remainder", the parent's width is determined by its own value plus those of its children. So, Enoch's width is 4/10ths of Awan's (4 / (6 + 4)).
Note that this means that the sum of the values of the children cannot exceed the value of their parent when branchvalues "total". When branchvalues "relative" (the default), children will not take up all of the space below their parent (unless the parent is the root and it has a value of 0).
import plotly.plotly as py
import plotly.graph_objs as go
trace = go.Sunburst(
labels=[ "Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
values=[ 65, 14, 12, 10, 2, 6, 6, 4, 4],
branchvalues="total",
outsidetextfont = {"size": 20, "color": "#377eb8"},
marker = {"line": {"width": 2}},
)
layout = go.Layout(
margin = go.layout.Margin(t=0, l=0, r=0, b=0)
)
py.iplot(go.Figure([trace], layout), filename='basic_sunburst_chart_total_branchvalues')
import plotly.plotly as py
import plotly.graph_objs as go
trace = go.Sunburst(
ids=[
"North America", "Europe", "Australia", "North America - Football", "Soccer",
"North America - Rugby", "Europe - Football", "Rugby",
"Europe - American Football","Australia - Football", "Association",
"Australian Rules", "Autstralia - American Football", "Australia - Rugby",
"Rugby League", "Rugby Union"
],
labels= [
"North<br>America", "Europe", "Australia", "Football", "Soccer", "Rugby",
"Football", "Rugby", "American<br>Football", "Football", "Association",
"Australian<br>Rules", "American<br>Football", "Rugby", "Rugby<br>League",
"Rugby<br>Union"
],
parents=[
"", "", "", "North America", "North America", "North America", "Europe",
"Europe", "Europe","Australia", "Australia - Football", "Australia - Football",
"Australia - Football", "Australia - Football", "Australia - Rugby",
"Australia - Rugby"
],
outsidetextfont={"size": 20, "color": "#377eb8"},
leaf={"opacity": 0.4},
marker={"line": {"width": 2}}
)
layout = go.Layout(
margin = go.layout.Margin(t=0, l=0, r=0, b=0),
sunburstcolorway=["#636efa","#ef553b","#00cc96"]
)
fig = go.Figure([trace], layout)
py.iplot(fig, filename='repeated_labels_sunburst')
This example uses a plotly grid attribute for the suplots. Reference the row and column destination using the domain attribute.
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
df1 = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')
df2 = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/coffee-flavors.csv')
trace1 = go.Sunburst(
ids=df1.ids,
labels=df1.labels,
parents=df1.parents,
domain=dict(column=0)
)
trace2 = go.Sunburst(
ids=df2.ids,
labels=df2.labels,
parents=df2.parents,
domain=dict(column=1),
maxdepth=2
)
layout = go.Layout(
grid=go.layout.Grid(columns=2, rows=1),
margin = go.layout.Margin(t=0, l=0, r=0, b=0),
sunburstcolorway=[
"#636efa","#EF553B","#00cc96","#ab63fa","#19d3f3",
"#e763fa", "#FECB52","#FFA15A","#FF6692","#B6E880"
],
extendsunburstcolors=True
)
data = [trace1, trace2]
fig = go.Figure(data, layout)
py.iplot(fig, filename='large_number_of_slices')
See https://plotly.com/python/reference/#sunburst for more information and chart attribute options!
from IPython.display import display, HTML
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
'sunburst-charts.ipynb', 'python/sunburst-charts/', 'Sunburst Charts',
'How to make Sunburst Charts.',
title= 'Sunburst in Python | plotly',
has_thumbnail='true', thumbnail='thumbnail/sunburst.gif',
language='python',
display_as='basic', order=6.1,
ipynb='~notebook_demo/274/')
Collecting git+https://github.com/plotly/publisher.git
Cloning https://github.com/plotly/publisher.git to /tmp/pip-req-build-nl381ouk
Building wheels for collected packages: publisher
Running setup.py bdist_wheel for publisher ... done
Stored in directory: /tmp/pip-ephem-wheel-cache-wbt7a39o/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966
Successfully built publisher
Installing collected packages: publisher
Found existing installation: publisher 0.13
Uninstalling publisher-0.13:
Successfully uninstalled publisher-0.13
Successfully installed publisher-0.13
You are using pip version 10.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.