diff --git a/doc/python/bar-charts.md b/doc/python/bar-charts.md index b0d04eef779..883140cb7e3 100644 --- a/doc/python/bar-charts.md +++ b/doc/python/bar-charts.md @@ -5,8 +5,8 @@ jupyter: text_representation: extension: .md format_name: markdown - format_version: "1.1" - jupytext_version: 1.1.1 + format_version: '1.2' + jupytext_version: 1.3.0 kernelspec: display_name: Python 3 language: python @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.6.8 + version: 3.7.3 plotly: description: How to make Bar Charts in Python with Plotly. display_as: basic @@ -177,6 +177,20 @@ fig = go.Figure(data=[go.Bar( fig.show() ``` +### Controlling text fontsize with uniformtext + +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`. + +```python +import plotly.express as px + +df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6") +fig = px.bar(df, y='pop', x='country', text='pop') +fig.update_traces(texttemplate='%{text:.2s}', textposition='outside') +fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide') +fig.show() +``` + ### Rotated Bar Chart Labels ```python diff --git a/doc/python/pie-charts.md b/doc/python/pie-charts.md index 9a1c7cae86c..a3fe1160c8f 100644 --- a/doc/python/pie-charts.md +++ b/doc/python/pie-charts.md @@ -122,6 +122,20 @@ fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20, fig.show() ``` +### Controlling text fontsize with uniformtext + +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. + +```python +import plotly.express as px + +df = px.data.gapminder().query("continent == 'Asia'") +fig = px.pie(df, values='pop', names='country') +fig.update_traces(textposition='inside') +fig.update_layout(uniformtext_minsize=12, uniformtext_mode='hide') +fig.show() +``` + #### Controlling text orientation inside pie sectors The `insidetextorientation` attribute controls the orientation of text inside sectors. With diff --git a/doc/python/sunburst-charts.md b/doc/python/sunburst-charts.md index c0b01c61895..11d162e6b15 100644 --- a/doc/python/sunburst-charts.md +++ b/doc/python/sunburst-charts.md @@ -201,6 +201,24 @@ fig.update_layout( fig.show() ``` +### Controlling text fontsize with uniformtext + +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. + +```python +import plotly.graph_objects as go +import pandas as pd + +df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv') + +fig = go.Figure(go.Sunburst( + ids = df.ids, + labels = df.labels, + parents = df.parents)) +fig.update_layout(uniformtext=dict(minsize=10, mode='hide')) +fig.show() +``` + ### Sunburst chart with a continuous colorscale 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. diff --git a/doc/python/text-and-annotations.md b/doc/python/text-and-annotations.md index 80892f3cb3e..6e86c1a0500 100644 --- a/doc/python/text-and-annotations.md +++ b/doc/python/text-and-annotations.md @@ -5,8 +5,8 @@ jupyter: text_representation: extension: .md format_name: markdown - format_version: "1.1" - jupytext_version: 1.2.1 + format_version: '1.2' + jupytext_version: 1.3.0 kernelspec: display_name: Python 3 language: python @@ -90,6 +90,43 @@ fig.add_trace(go.Scatter( fig.show() ``` +### Controlling text fontsize with uniformtext + +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. + +```python +import plotly.express as px + +df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6") +fig = px.bar(df, y='pop', x='country', text='pop') +fig.update_traces(texttemplate='%{text:.2s}', textposition='outside') +fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide') +fig.show() +``` + +```python +import plotly.express as px + +df = px.data.gapminder().query("continent == 'Asia' and year == 2007") +fig = px.pie(df, values='pop', names='country') +fig.update_traces(textposition='inside') +fig.update_layout(uniformtext_minsize=12, uniformtext_mode='hide') +fig.show() +``` + +### Controlling text fontsize with textfont + +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. + +```python +import plotly.express as px + +df = px.data.gapminder().query("continent == 'Asia' and year == 2007") +fig = px.pie(df, values='pop', names='country') +fig.update_traces(textposition='inside', textfont_size=14) +fig.show() +``` + ### Adding Hover Text to Data in Line and Scatter Plots ```python diff --git a/doc/python/treemaps.md b/doc/python/treemaps.md index 02167e3cdb8..15def761087 100644 --- a/doc/python/treemaps.md +++ b/doc/python/treemaps.md @@ -287,5 +287,23 @@ fig.update_layout( fig.show() ``` +### Controlling text fontsize with uniformtext + +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. + +```python +import plotly.graph_objects as go +import pandas as pd + +df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv') + +fig = go.Figure(go.Treemap( + ids = df.ids, + labels = df.labels, + parents = df.parents)) +fig.update_layout(uniformtext=dict(minsize=10, mode='hide')) +fig.show() +``` + #### Reference See https://plot.ly/python/reference/#treemap for more information and chart attribute options!