Skip to content

Commit a226a5f

Browse files
documenting facet_*_spacing
1 parent c33835e commit a226a5f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Diff for: doc/python/facet-plots.md

+21-4
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.4
9+
jupytext_version: 1.4.2
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.0
23+
version: 3.7.7
2424
plotly:
2525
description: How to make Facet and Trellis Plots in Python with Plotly.
2626
display_as: statistical
@@ -103,7 +103,7 @@ fig.show()
103103

104104
### Customize Subplot Figure Titles
105105

106-
Since subplot figure titles are [annotations](https://plotly.com/python/text-and-annotations/#simple-annotation), you can use the `for_each_annotation` function to customize them.
106+
Since subplot figure titles are [annotations](https://plotly.com/python/text-and-annotations/#simple-annotation), you can use the `for_each_annotation` function to customize them, for example to remove the equal-sign (`=`).
107107

108108
In the following example, we pass a lambda function to `for_each_annotation` in order to change the figure subplot titles from `smoker=No` and `smoker=Yes` to just `No` and `Yes`.
109109

@@ -115,8 +115,25 @@ fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
115115
fig.show()
116116
```
117117

118+
### Controlling Facet Spacing
119+
120+
The `facet_row_spacing` and `facet_col_spacing` arguments can be used to control the spacing between rows and columns. These values are specified in fractions of the plotting area in paper coordinates and not in pixels, so they will grow or shrink with the `width` and `height` of the figure.
121+
122+
The defaults work well with 1-4 rows or columns at the default figure size with the default font size, but need to be reduced to around 0.01 for very large figures or figures with many rows or columns. Conversely, if activating tick labels on all facets, the spacing will need to be increased.
123+
118124
```python
125+
import plotly.express as px
119126

127+
df = px.data.gapminder().query("continent == 'Africa'")
128+
129+
fig = px.line(df, x="year", y="lifeExp", facet_col="country", facet_col_wrap=7,
130+
facet_row_spacing=0.04, # default is 0.07 when facet_col_wrap is used
131+
facet_col_spacing=0.04, # default is 0.03
132+
height=600, width=800,
133+
title="Life Expectancy in Africa")
134+
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
135+
fig.update_yaxes(showticklabels=True)
136+
fig.show()
120137
```
121138

122139
### Synchronizing axes in subplots with `matches`
@@ -138,4 +155,4 @@ for i in range(1, 4):
138155
fig.add_trace(go.Scatter(x=x, y=np.random.random(N)), 1, i)
139156
fig.update_xaxes(matches='x')
140157
fig.show()
141-
```
158+
```

Diff for: packages/python/plotly/plotly/express/_doc.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,12 @@
226226
],
227227
facet_row_spacing=[
228228
"float between 0 and 1",
229-
"Spacing between facet rows, in paper units.",
229+
"Spacing between facet rows, in paper units. Default is 0.03 or 0.0.7 when facet_col_wrap is used.",
230+
],
231+
facet_col_spacing=[
232+
"float between 0 and 1",
233+
"Spacing between facet columns, in paper units Default is 0.02.",
230234
],
231-
facet_col_spacing=["int", "Spacing between facet columns, in paper units",],
232235
animation_frame=[
233236
colref_type,
234237
colref_desc,

0 commit comments

Comments
 (0)