Skip to content

Commit 3babd2e

Browse files
committed
Merge branch 'master' into 4.5-release
2 parents a5bb459 + 705ec3b commit 3babd2e

File tree

9 files changed

+519
-7
lines changed

9 files changed

+519
-7
lines changed

doc/python/bar-charts.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: "1.1"
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
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.6.8
23+
version: 3.7.3
2424
plotly:
2525
description: How to make Bar Charts in Python with Plotly.
2626
display_as: basic
@@ -177,6 +177,20 @@ fig = go.Figure(data=[go.Bar(
177177
fig.show()
178178
```
179179

180+
### Controlling text fontsize with uniformtext
181+
182+
If you want all the text labels to have the same size, you can use the `uniformtext` layout parameter. The `minsize` attribute sets the font size, and the `mode` attribute sets what happens for labels which cannot fit with the desired fontsize: either `hide` them or `show` them with overflow. In the example below we also force the text to be outside of bars with `textposition`.
183+
184+
```python
185+
import plotly.express as px
186+
187+
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
188+
fig = px.bar(df, y='pop', x='country', text='pop')
189+
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
190+
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
191+
fig.show()
192+
```
193+
180194
### Rotated Bar Chart Labels
181195

182196
```python

doc/python/pie-charts.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20,
122122
fig.show()
123123
```
124124

125+
### Controlling text fontsize with uniformtext
126+
127+
If you want all the text labels to have the same size, you can use the `uniformtext` layout parameter. The `minsize` attribute sets the font size, and the `mode` attribute sets what happens for labels which cannot fit with the desired fontsize: either `hide` them or `show` them with overflow. In the example below we also force the text to be inside with `textposition`, otherwise text labels which do not fit are displayed outside of pie sectors.
128+
129+
```python
130+
import plotly.express as px
131+
132+
df = px.data.gapminder().query("continent == 'Asia'")
133+
fig = px.pie(df, values='pop', names='country')
134+
fig.update_traces(textposition='inside')
135+
fig.update_layout(uniformtext_minsize=12, uniformtext_mode='hide')
136+
fig.show()
137+
```
138+
125139
#### Controlling text orientation inside pie sectors
126140

127141
The `insidetextorientation` attribute controls the orientation of text inside sectors. With

doc/python/sunburst-charts.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,53 @@ fig =px.sunburst(
6262
fig.show()
6363
```
6464

65+
### Sunburst of a rectangular DataFrame with plotly.express
66+
67+
Hierarchical data are often stored as a rectangular dataframe, with different columns corresponding to different levels of the hierarchy. `px.sunburst` can take a `path` parameter corresponding to a list of columns. Note that `id` and `parent` should not be provided if `path` is given.
68+
69+
```python
70+
import plotly.express as px
71+
df = px.data.tips()
72+
fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill')
73+
fig.show()
74+
```
75+
76+
### Sunburst of a rectangular DataFrame with continuous color argument in px.sunburst
77+
78+
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.
79+
80+
```python
81+
import plotly.express as px
82+
import numpy as np
83+
df = px.data.gapminder().query("year == 2007")
84+
fig = px.sunburst(df, path=['continent', 'country'], values='pop',
85+
color='lifeExp', hover_data=['iso_alpha'],
86+
color_continuous_scale='RdBu',
87+
color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
88+
fig.show()
89+
```
90+
91+
### Rectangular data with missing values
92+
93+
If the dataset is not fully rectangular, missing values should be supplied as `None`. Note that the parents of `None` entries must be a leaf, i.e. it cannot have other children than `None` (otherwise a `ValueError` is raised).
94+
95+
```python
96+
import plotly.express as px
97+
import pandas as pd
98+
vendors = ["A", "B", "C", "D", None, "E", "F", "G", "H", None]
99+
sectors = ["Tech", "Tech", "Finance", "Finance", "Other",
100+
"Tech", "Tech", "Finance", "Finance", "Other"]
101+
regions = ["North", "North", "North", "North", "North",
102+
"South", "South", "South", "South", "South"]
103+
sales = [1, 3, 2, 4, 1, 2, 2, 1, 4, 1]
104+
df = pd.DataFrame(
105+
dict(vendors=vendors, sectors=sectors, regions=regions, sales=sales)
106+
)
107+
print(df)
108+
fig = px.sunburst(df, path=['regions', 'sectors', 'vendors'], values='sales')
109+
fig.show()
110+
```
111+
65112
### Basic Sunburst Plot with go.Sunburst
66113

67114
If Plotly Express does not provide a good starting point, it is also possible to use the more generic `go.Sunburst` function from `plotly.graph_objects`.
@@ -201,6 +248,24 @@ fig.update_layout(
201248
fig.show()
202249
```
203250

251+
### Controlling text fontsize with uniformtext
252+
253+
If you want all the text labels to have the same size, you can use the `uniformtext` layout parameter. The `minsize` attribute sets the font size, and the `mode` attribute sets what happens for labels which cannot fit with the desired fontsize: either `hide` them or `show` them with overflow.
254+
255+
```python
256+
import plotly.graph_objects as go
257+
import pandas as pd
258+
259+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')
260+
261+
fig = go.Figure(go.Sunburst(
262+
ids = df.ids,
263+
labels = df.labels,
264+
parents = df.parents))
265+
fig.update_layout(uniformtext=dict(minsize=10, mode='hide'))
266+
fig.show()
267+
```
268+
204269
### Sunburst chart with a continuous colorscale
205270

206271
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.

doc/python/text-and-annotations.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: "1.1"
9-
jupytext_version: 1.2.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -90,6 +90,43 @@ fig.add_trace(go.Scatter(
9090
fig.show()
9191
```
9292

93+
### Controlling text fontsize with uniformtext
94+
95+
For the [pie](/python/pie-charts), [bar](/python/bar-charts), [sunburst](/python/sunburst-charts) and [treemap](/python/treemap-charts) traces, it is possible to force all the text labels to have the same size thanks to the `uniformtext` layout parameter. The `minsize` attribute sets the font size, and the `mode` attribute sets what happens for labels which cannot fit with the desired fontsize: either `hide` them or `show` them with overflow.
96+
97+
```python
98+
import plotly.express as px
99+
100+
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
101+
fig = px.bar(df, y='pop', x='country', text='pop')
102+
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
103+
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
104+
fig.show()
105+
```
106+
107+
```python
108+
import plotly.express as px
109+
110+
df = px.data.gapminder().query("continent == 'Asia' and year == 2007")
111+
fig = px.pie(df, values='pop', names='country')
112+
fig.update_traces(textposition='inside')
113+
fig.update_layout(uniformtext_minsize=12, uniformtext_mode='hide')
114+
fig.show()
115+
```
116+
117+
### Controlling text fontsize with textfont
118+
119+
The `textfont_size` parameter of the the [pie](/python/pie-charts), [bar](/python/bar-charts), [sunburst](/python/sunburst-charts) and [treemap](/python/treemap-charts) traces can be used to set the **maximum font size** used in the chart. Note that the `textfont` parameter sets the `insidetextfont` and `outsidetextfont` parameter, which can also be set independently.
120+
121+
```python
122+
import plotly.express as px
123+
124+
df = px.data.gapminder().query("continent == 'Asia' and year == 2007")
125+
fig = px.pie(df, values='pop', names='country')
126+
fig.update_traces(textposition='inside', textfont_size=14)
127+
fig.show()
128+
```
129+
93130
### Adding Hover Text to Data in Line and Scatter Plots
94131

95132
```python

doc/python/treemaps.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,52 @@ fig = px.treemap(
5151
fig.show()
5252
```
5353

54+
### Treemap of a rectangular DataFrame with plotly.express
55+
56+
Hierarchical data are often stored as a rectangular dataframe, with different columns corresponding to different levels of the hierarchy. `px.treemap` can take a `path` parameter corresponding to a list of columns. Note that `id` and `parent` should not be provided if `path` is given.
57+
58+
```python
59+
import plotly.express as px
60+
df = px.data.tips()
61+
fig = px.treemap(df, path=['day', 'time', 'sex'], values='total_bill')
62+
fig.show()
63+
```
64+
65+
### Treemap of a rectangular DataFrame with continuous color argument in px.treemap
66+
67+
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.
68+
69+
```python
70+
import plotly.express as px
71+
import numpy as np
72+
df = px.data.gapminder().query("year == 2007")
73+
fig = px.treemap(df, path=['continent', 'country'], values='pop',
74+
color='lifeExp', hover_data=['iso_alpha'],
75+
color_continuous_scale='RdBu',
76+
color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
77+
fig.show()
78+
```
79+
80+
### Rectangular data with missing values
81+
82+
If the dataset is not fully rectangular, missing values should be supplied as `None`.
83+
84+
```python
85+
import plotly.express as px
86+
import pandas as pd
87+
vendors = ["A", "B", "C", "D", None, "E", "F", "G", "H", None]
88+
sectors = ["Tech", "Tech", "Finance", "Finance", "Other",
89+
"Tech", "Tech", "Finance", "Finance", "Other"]
90+
regions = ["North", "North", "North", "North", "North",
91+
"South", "South", "South", "South", "South"]
92+
sales = [1, 3, 2, 4, 1, 2, 2, 1, 4, 1]
93+
df = pd.DataFrame(
94+
dict(vendors=vendors, sectors=sectors, regions=regions, sales=sales)
95+
)
96+
print(df)
97+
fig = px.treemap(df, path=['regions', 'sectors', 'vendors'], values='sales')
98+
fig.show()
99+
```
54100
### Basic Treemap with go.Treemap
55101

56102
If Plotly Express does not provide a good starting point, it is also possible to use the more generic `go.Treemap` function from `plotly.graph_objects`.
@@ -287,5 +333,23 @@ fig.update_layout(
287333
fig.show()
288334
```
289335

336+
### Controlling text fontsize with uniformtext
337+
338+
If you want all the text labels to have the same size, you can use the `uniformtext` layout parameter. The `minsize` attribute sets the font size, and the `mode` attribute sets what happens for labels which cannot fit with the desired fontsize: either `hide` them or `show` them with overflow.
339+
340+
```python
341+
import plotly.graph_objects as go
342+
import pandas as pd
343+
344+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')
345+
346+
fig = go.Figure(go.Treemap(
347+
ids = df.ids,
348+
labels = df.labels,
349+
parents = df.parents))
350+
fig.update_layout(uniformtext=dict(minsize=10, mode='hide'))
351+
fig.show()
352+
```
353+
290354
#### Reference
291355
See https://plot.ly/python/reference/#treemap for more information and chart attribute options!

packages/python/plotly/plotly/express/_chart_types.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,7 @@ def sunburst(
12701270
names=None,
12711271
values=None,
12721272
parents=None,
1273+
path=None,
12731274
ids=None,
12741275
color=None,
12751276
color_continuous_scale=None,
@@ -1296,6 +1297,13 @@ def sunburst(
12961297
layout_patch = {"sunburstcolorway": color_discrete_sequence}
12971298
else:
12981299
layout_patch = {}
1300+
if path is not None and (ids is not None or parents is not None):
1301+
raise ValueError(
1302+
"Either `path` should be provided, or `ids` and `parents`."
1303+
"These parameters are mutually exclusive and cannot be passed together."
1304+
)
1305+
if path is not None and branchvalues is None:
1306+
branchvalues = "total"
12991307
return make_figure(
13001308
args=locals(),
13011309
constructor=go.Sunburst,
@@ -1313,6 +1321,7 @@ def treemap(
13131321
values=None,
13141322
parents=None,
13151323
ids=None,
1324+
path=None,
13161325
color=None,
13171326
color_continuous_scale=None,
13181327
range_color=None,
@@ -1338,6 +1347,13 @@ def treemap(
13381347
layout_patch = {"treemapcolorway": color_discrete_sequence}
13391348
else:
13401349
layout_patch = {}
1350+
if path is not None and (ids is not None or parents is not None):
1351+
raise ValueError(
1352+
"Either `path` should be provided, or `ids` and `parents`."
1353+
"These parameters are mutually exclusive and cannot be passed together."
1354+
)
1355+
if path is not None and branchvalues is None:
1356+
branchvalues = "total"
13411357
return make_figure(
13421358
args=locals(),
13431359
constructor=go.Treemap,

0 commit comments

Comments
 (0)