#!/usr/bin/env python
# coding: utf-8
# #### New to Plotly?
# Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).
# You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).
# We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
#
# #### Version Check
# Note: Sunburst Charts are available in version 3.8+
# Run `pip install plotly --upgrade` to update your Plotly version
#
#
# In[1]:
import plotly
plotly.__version__
# ### Basic Sunburst Plot ###
# In[2]:
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')
# ### Branchvalues ###
# 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).
# In[3]:
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')
# ### Sunburst with Repeated Labels
# In[4]:
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 America", "Europe", "Australia", "Football", "Soccer", "Rugby",
"Football", "Rugby", "American Football", "Football", "Association",
"Australian Rules", "American Football", "Rugby", "Rugby League",
"Rugby 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')
# ### Large Number of Slices
# This example uses a [plotly grid attribute](https://plotly.com/python/reference/#layout-grid) for the suplots. Reference the row and column destination using the [domain](https://plotly.com/python/reference/#sunburst-domain) attribute.
# In[5]:
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')
# #### Reference
# See https://plotly.com/python/reference/#sunburst for more information and chart attribute options!
# In[5]:
from IPython.display import display, HTML
display(HTML(''))
display(HTML(''))
get_ipython().system(' 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/')
# In[ ]: