Skip to content

Commit 9ba39c1

Browse files
Merge pull request #177 from plotly/sunburst-continuous-colorscale
Sunburst + treemap continuous colorscale example
2 parents 13d3e8e + 6a9a960 commit 9ba39c1

File tree

2 files changed

+161
-1
lines changed

2 files changed

+161
-1
lines changed

Diff for: python/sunburst-charts.md

+80
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,85 @@ fig.update_layout(
153153
fig.show()
154154
```
155155

156+
### Sunburst chart with a continuous colorscale
157+
158+
The example below visualizes a breakdown of sales (corresponding to sector width) and call success rate (corresponding to sector color) by region, county and salesperson level. For example, when exploring the data you can see that although the East region is behaving poorly, the Tyler county is still above average -- however, its performance is reduced by the poor success rate of salesperson GT.
159+
160+
In the right subplot which has a `maxdepth` of two levels, click on a sector to see its breakdown to lower levels.
161+
162+
```python
163+
import plotly.graph_objects as go
164+
from plotly.subplots import make_subplots
165+
import pandas as pd
166+
167+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/sales_success.csv')
168+
print(df.head())
169+
170+
levels = ['salesperson', 'county', 'region'] # levels used for the hierarchical chart
171+
color_columns = ['sales', 'calls']
172+
value_column = 'calls'
173+
174+
def build_hierarchical_dataframe(df, levels, value_column, color_columns=None):
175+
"""
176+
Build a hierarchy of levels for Sunburst or Treemap charts.
177+
178+
Levels are given starting from the bottom to the top of the hierarchy,
179+
ie the last level corresponds to the root.
180+
"""
181+
df_all_trees = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
182+
for i, level in enumerate(levels):
183+
df_tree = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
184+
dfg = df.groupby(levels[i:]).sum(numerical_only=True)
185+
dfg = dfg.reset_index()
186+
df_tree['id'] = dfg[level].copy()
187+
if i < len(levels) - 1:
188+
df_tree['parent'] = dfg[levels[i+1]].copy()
189+
else:
190+
df_tree['parent'] = 'total'
191+
df_tree['value'] = dfg[value_column]
192+
df_tree['color'] = dfg[color_columns[0]] / dfg[color_columns[1]]
193+
df_all_trees = df_all_trees.append(df_tree, ignore_index=True)
194+
total = pd.Series(dict(id='total', parent='',
195+
value=df[value_column].sum(),
196+
color=df[color_columns[0]].sum() / df[color_columns[1]].sum()))
197+
df_all_trees = df_all_trees.append(total, ignore_index=True)
198+
return df_all_trees
199+
200+
201+
df_all_trees = build_hierarchical_dataframe(df, levels, value_column, color_columns)
202+
average_score = df['sales'].sum() / df['calls'].sum()
203+
204+
fig = make_subplots(1, 2, specs=[[{"type": "domain"}, {"type": "domain"}]],)
205+
206+
fig.add_trace(go.Sunburst(
207+
labels=df_all_trees['id'],
208+
parents=df_all_trees['parent'],
209+
values=df_all_trees['value'],
210+
branchvalues='total',
211+
marker=dict(
212+
colors=df_all_trees['color'],
213+
colorscale='RdBu',
214+
cmid=average_score),
215+
hovertemplate='<b>%{label} </b> <br> Sales: %{value}<br> Success rate: %{color:.2f}',
216+
name=''
217+
), 1, 1)
218+
219+
fig.add_trace(go.Sunburst(
220+
labels=df_all_trees['id'],
221+
parents=df_all_trees['parent'],
222+
values=df_all_trees['value'],
223+
branchvalues='total',
224+
marker=dict(
225+
colors=df_all_trees['color'],
226+
colorscale='RdBu',
227+
cmid=average_score),
228+
hovertemplate='<b>%{label} </b> <br> Sales: %{value}<br> Success rate: %{color:.2f}',
229+
maxdepth=2
230+
), 1, 2)
231+
232+
fig.update_layout(margin=dict(t=10, b=10, r=10, l=10))
233+
fig.show()
234+
```
235+
156236
#### Reference
157237
See https://plot.ly/python/reference/#sunburst for more information and chart attribute options!

Diff for: python/treemaps.md

+81-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.1'
9-
jupytext_version: 1.2.1
9+
jupytext_version: 1.1.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -147,6 +147,86 @@ fig = go.Figure(go.Treemap(
147147
fig.show()
148148
```
149149

150+
### Treemap chart with a continuous colorscale
151+
152+
The example below visualizes a breakdown of sales (corresponding to sector width) and call success rate (corresponding to sector color) by region, county and salesperson level. For example, when exploring the data you can see that although the East region is behaving poorly, the Tyler county is still above average -- however, its performance is reduced by the poor success rate of salesperson GT.
153+
154+
In the right subplot which has a `maxdepth` of two levels, click on a sector to see its breakdown to lower levels.
155+
156+
```python
157+
import plotly.graph_objects as go
158+
from plotly.subplots import make_subplots
159+
import pandas as pd
160+
161+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/sales_success.csv')
162+
print(df.head())
163+
164+
levels = ['salesperson', 'county', 'region'] # levels used for the hierarchical chart
165+
color_columns = ['sales', 'calls']
166+
value_column = 'calls'
167+
168+
def build_hierarchical_dataframe(df, levels, value_column, color_columns=None):
169+
"""
170+
Build a hierarchy of levels for Sunburst or Treemap charts.
171+
172+
Levels are given starting from the bottom to the top of the hierarchy,
173+
ie the last level corresponds to the root.
174+
"""
175+
df_all_trees = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
176+
for i, level in enumerate(levels):
177+
df_tree = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
178+
dfg = df.groupby(levels[i:]).sum(numerical_only=True)
179+
dfg = dfg.reset_index()
180+
df_tree['id'] = dfg[level].copy()
181+
if i < len(levels) - 1:
182+
df_tree['parent'] = dfg[levels[i+1]].copy()
183+
else:
184+
df_tree['parent'] = 'total'
185+
df_tree['value'] = dfg[value_column]
186+
df_tree['color'] = dfg[color_columns[0]] / dfg[color_columns[1]]
187+
df_all_trees = df_all_trees.append(df_tree, ignore_index=True)
188+
total = pd.Series(dict(id='total', parent='',
189+
value=df[value_column].sum(),
190+
color=df[color_columns[0]].sum() / df[color_columns[1]].sum()))
191+
df_all_trees = df_all_trees.append(total, ignore_index=True)
192+
return df_all_trees
193+
194+
195+
df_all_trees = build_hierarchical_dataframe(df, levels, value_column, color_columns)
196+
average_score = df['sales'].sum() / df['calls'].sum()
197+
198+
fig = make_subplots(1, 2, specs=[[{"type": "domain"}, {"type": "domain"}]],)
199+
200+
fig.add_trace(go.Treemap(
201+
labels=df_all_trees['id'],
202+
parents=df_all_trees['parent'],
203+
values=df_all_trees['value'],
204+
branchvalues='total',
205+
marker=dict(
206+
colors=df_all_trees['color'],
207+
colorscale='RdBu',
208+
cmid=average_score),
209+
hovertemplate='<b>%{label} </b> <br> Sales: %{value}<br> Success rate: %{color:.2f}',
210+
name=''
211+
), 1, 1)
212+
213+
fig.add_trace(go.Treemap(
214+
labels=df_all_trees['id'],
215+
parents=df_all_trees['parent'],
216+
values=df_all_trees['value'],
217+
branchvalues='total',
218+
marker=dict(
219+
colors=df_all_trees['color'],
220+
colorscale='RdBu',
221+
cmid=average_score),
222+
hovertemplate='<b>%{label} </b> <br> Sales: %{value}<br> Success rate: %{color:.2f}',
223+
maxdepth=2
224+
), 1, 2)
225+
226+
fig.update_layout(margin=dict(t=10, b=10, r=10, l=10))
227+
fig.show()
228+
```
229+
150230
### Nested Layers in Treemap
151231

152232
The following example uses hierarchical data that includes layers and grouping. Treemap and [Sunburst](https://plot.ly/python/sunburst-charts/) charts reveal insights into the data, and the format of your hierarchical data. [maxdepth](https://plot.ly/python/reference/#treemap-maxdepth) attribute sets the number of rendered sectors from the given level.

0 commit comments

Comments
 (0)