Skip to content

Commit c54fac2

Browse files
dummy root node in treemaps
1 parent 10b9d55 commit c54fac2

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

‎doc/python/treemaps.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.2'
9-
jupytext_version: 1.3.0
9+
jupytext_version: 1.3.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.3
23+
version: 3.6.8
2424
plotly:
2525
description: How to make Treemap Charts with Plotly
2626
display_as: basic
@@ -66,11 +66,14 @@ fig.show()
6666

6767
If a `color` argument is passed, the color of a node is computed as the average of the color values of its children, weighted by their values.
6868

69+
**Note**: for best results, ensure that the first `path` element is a single root node. In the examples below we are creating a dummy column containing identical values for each row to achieve this.
70+
6971
```python
7072
import plotly.express as px
7173
import numpy as np
7274
df = px.data.gapminder().query("year == 2007")
73-
fig = px.treemap(df, path=['continent', 'country'], values='pop',
75+
df["world"] = "world" # in order to have a single root node
76+
fig = px.treemap(df, path=['world', 'continent', 'country'], values='pop',
7477
color='lifeExp', hover_data=['iso_alpha'],
7578
color_continuous_scale='RdBu',
7679
color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
@@ -84,7 +87,8 @@ When the argument of `color` corresponds to non-numerical data, discrete colors
8487
```python
8588
import plotly.express as px
8689
df = px.data.tips()
87-
fig = px.treemap(df, path=['sex', 'day', 'time'], values='total_bill', color='day')
90+
df["all"] = "all" # in order to have a single root node
91+
fig = px.treemap(df, path=['all', 'sex', 'day', 'time'], values='total_bill', color='day')
8892
fig.show()
8993
```
9094

@@ -93,7 +97,8 @@ In the example below the color of Saturday and Sunday sectors is the same as Din
9397
```python
9498
import plotly.express as px
9599
df = px.data.tips()
96-
fig = px.treemap(df, path=['sex', 'day', 'time'], values='total_bill', color='time')
100+
df["all"] = "all" # in order to have a single root node
101+
fig = px.treemap(df, path=['all', 'sex', 'day', 'time'], values='total_bill', color='time')
97102
fig.show()
98103
```
99104

@@ -113,8 +118,9 @@ sales = [1, 3, 2, 4, 1, 2, 2, 1, 4, 1]
113118
df = pd.DataFrame(
114119
dict(vendors=vendors, sectors=sectors, regions=regions, sales=sales)
115120
)
121+
df["all"] = "all" # in order to have a single root node
116122
print(df)
117-
fig = px.treemap(df, path=['regions', 'sectors', 'vendors'], values='sales')
123+
fig = px.treemap(df, path=['all', 'regions', 'sectors', 'vendors'], values='sales')
118124
fig.show()
119125
```
120126
### Basic Treemap with go.Treemap

0 commit comments

Comments
 (0)
X