Skip to content

Commit 9e1f1c2

Browse files
Merge pull request #2095 from plotly/uniformtext
uniformtext examples
2 parents 60cec29 + aaf7494 commit 9e1f1c2

File tree

5 files changed

+106
-5
lines changed

5 files changed

+106
-5
lines changed

doc/python/bar-charts.md

+17-3
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

+14
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

+18
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ fig.update_layout(
201201
fig.show()
202202
```
203203

204+
### Controlling text fontsize with uniformtext
205+
206+
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.
207+
208+
```python
209+
import plotly.graph_objects as go
210+
import pandas as pd
211+
212+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')
213+
214+
fig = go.Figure(go.Sunburst(
215+
ids = df.ids,
216+
labels = df.labels,
217+
parents = df.parents))
218+
fig.update_layout(uniformtext=dict(minsize=10, mode='hide'))
219+
fig.show()
220+
```
221+
204222
### Sunburst chart with a continuous colorscale
205223

206224
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

+39-2
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

+18
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,23 @@ fig.update_layout(
287287
fig.show()
288288
```
289289

290+
### Controlling text fontsize with uniformtext
291+
292+
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.
293+
294+
```python
295+
import plotly.graph_objects as go
296+
import pandas as pd
297+
298+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')
299+
300+
fig = go.Figure(go.Treemap(
301+
ids = df.ids,
302+
labels = df.labels,
303+
parents = df.parents))
304+
fig.update_layout(uniformtext=dict(minsize=10, mode='hide'))
305+
fig.show()
306+
```
307+
290308
#### Reference
291309
See https://plot.ly/python/reference/#treemap for more information and chart attribute options!

0 commit comments

Comments
 (0)