|
6 | 6 | extension: .md
|
7 | 7 | format_name: markdown
|
8 | 8 | format_version: '1.1'
|
9 |
| - jupytext_version: 1.2.1 |
| 9 | + jupytext_version: 1.1.1 |
10 | 10 | kernelspec:
|
11 | 11 | display_name: Python 3
|
12 | 12 | language: python
|
@@ -147,6 +147,86 @@ fig = go.Figure(go.Treemap(
|
147 | 147 | fig.show()
|
148 | 148 | ```
|
149 | 149 |
|
| 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 | + |
150 | 230 | ### Nested Layers in Treemap
|
151 | 231 |
|
152 | 232 | 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