Skip to content

Commit 7a3364a

Browse files
titles
1 parent 3f339b7 commit 7a3364a

File tree

2 files changed

+144
-129
lines changed

2 files changed

+144
-129
lines changed

Diff for: doc/python/axes.md

+127-126
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,105 @@ fig.show()
8181

8282
The different groups of Cartesian axes properties are
8383

84+
- title of the axis
8485
- tick values (locations of tick marks) and tick labels. Tick labels and grid lines are placed at tick values.
8586
- lines: grid lines (passing through tick values), axis lines, zero lines
86-
- title of the axis
8787
- range of the axis
8888
- domain of the axis
8989

9090
The examples on this page apply to axes of any type, but extra attributes are available for [axes of type `category`](/pythone/categorical-axes/) and [axes of type `date`](/python/time-series/).
9191

92+
93+
#### Set and Style Axes Title Labels
94+
95+
##### Set axis title text with Plotly Express
96+
97+
Axis titles are automatically set to the column names when [using Plotly Express with a data frame as input](/python/px-arguments/).
98+
99+
```python
100+
import plotly.express as px
101+
df = px.data.tips()
102+
fig = px.scatter(df, x="total_bill", y="tip", color="sex")
103+
fig.show()
104+
```
105+
106+
Axis titles (and [legend titles](/python/legend/)) can also be overridden using the `labels` argument of Plotly Express functions:
107+
108+
```python
109+
import plotly.express as px
110+
df = px.data.tips()
111+
fig = px.scatter(df, x="total_bill", y="tip", color="sex",
112+
labels=dict(total_bill="Total Bill ($)", tip="Tip ($)", sex="Payer Gender")
113+
)
114+
fig.show()
115+
```
116+
117+
The PX `labels` argument can also be used without a data frame argument:
118+
119+
120+
```python
121+
import plotly.express as px
122+
fig = px.bar(df, x=["Apples", "Oranges"], y=[10,20], color=["Here", "There"],
123+
labels=dict(x="Fruit", y="Amount", color="Place")
124+
)
125+
fig.show()
126+
```
127+
128+
##### Set axis title text with Graph Objects
129+
130+
Axis titles are set using the nested `title.text` property of the x or y axis. Here is an example of creating a new figure and using `update_xaxes` and `update_yaxes`, with magic underscore notation, to set the axis titles.
131+
132+
```python
133+
import plotly.express as px
134+
135+
fig = px.line(y=[1, 0])
136+
137+
fig.update_xaxes(title_text='Time')
138+
fig.update_yaxes(title_text='Value A')
139+
140+
fig.show()
141+
```
142+
143+
### Set axis title position
144+
145+
This example sets `standoff` attribute to cartesian axes to determine the distance between the tick labels and the axis title. Note that the axis title position is always constrained within the margins, so the actual standoff distance is always less than the set or default value. By default [automargin](https://plotly.com/python/setting-graph-size/#automatically-adjust-margins) is `True` in Plotly template for the cartesian axis, so the margins will be pushed to fit the axis title at given standoff distance.
146+
147+
```python
148+
import plotly.graph_objects as go
149+
150+
fig = go.Figure(go.Scatter(
151+
mode = "lines+markers",
152+
y = [4, 1, 3],
153+
x = ["December", "January", "February"]))
154+
155+
fig.update_xaxes(
156+
tickangle = 90,
157+
title_text = "Month",
158+
title_font = {"size": 20},
159+
title_standoff = 25)
160+
161+
fig.update_yaxes(
162+
title_text = "Temperature",
163+
title_standoff = 25)
164+
165+
fig.show()
166+
```
167+
168+
##### Set axis title font
169+
170+
Here is an example that configures the font family, size, and color for the axis titles in a figure created using Plotly Express.
171+
172+
```python
173+
import plotly.express as px
174+
df = px.data.iris()
175+
176+
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
177+
fig.update_xaxes(title_font=dict(size=18, family='Courier', color='crimson'))
178+
fig.update_yaxes(title_font=dict(size=18, family='Courier', color='crimson'))
179+
180+
fig.show()
181+
```
182+
92183
#### Tick Placement, Color, and Style
93184

94185
##### Toggling axis tick marks
@@ -262,7 +353,8 @@ Here is an example of setting `showgrid` to `False` in the graph object figure c
262353
import plotly.express as px
263354

264355
fig = px.line(y=[1, 0])
265-
fig.update_layout(xaxis_showgrid=False, yaxis_showgrid=False)
356+
fig.update_xaxes(showgrid=False)
357+
fig.update_yaxes(showgrid=False)
266358
fig.show()
267359
```
268360

@@ -275,43 +367,8 @@ import plotly.express as px
275367

276368
fig = px.line(y=[1, 0])
277369

278-
fig.update_layout(
279-
xaxis=dict(showgrid=False, zeroline=False),
280-
yaxis=dict(showgrid=False, zeroline=False),
281-
)
282-
fig.show()
283-
```
284-
285-
##### Toggle grid and zerolines with update axis methods
286-
287-
Axis properties can be also updated for figures after they are constructed using the `update_xaxes` and `update_yaxes` graph object figure methods.
288-
289-
Here is an example that disables the x and y axis grid and zero lines using `update_xaxes` and `update_yaxes`.
290-
291-
```python
292-
import plotly.express as px
293-
294-
fig = px.line(y=[1, 0])
295-
fig.update_xaxes(showgrid=False, zeroline=False)
296-
fig.update_yaxes(showgrid=False, zeroline=False)
297-
298-
fig.show()
299-
```
300-
301-
##### Toggle grid and zerolines for figure created with Plotly Express
302-
303-
An advantage of using the `update_xaxis` and `update_yaxis` methods is that these updates will (by default) apply to all axes in the figure. This is especially useful when customizing figures created using Plotly Express, figure factories, or `make_subplots`.
304-
305-
Here is an example of disabling all grid and zero lines in a faceted figure created by Plotly Express.
306-
307-
```python
308-
import plotly.express as px
309-
df = px.data.iris()
310-
311-
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
312370
fig.update_xaxes(showgrid=False, zeroline=False)
313371
fig.update_yaxes(showgrid=False, zeroline=False)
314-
315372
fig.show()
316373
```
317374

@@ -385,63 +442,6 @@ fig.update_yaxes(zeroline=True, zerolinewidth=2, zerolinecolor='LightPink')
385442
fig.show()
386443
```
387444

388-
#### Set and Style Axes Title Labels
389-
390-
##### Set axis title text
391-
392-
Axis titles are set using the nested `title.text` property of the x or y axis. Here is an example of creating a new figure and using `update_xaxes` and `update_yaxes`, with magic underscore notation, to set the axis titles.
393-
394-
```python
395-
import plotly.express as px
396-
397-
fig = px.line(y=[1, 0])
398-
399-
fig.update_xaxes(title_text='Time')
400-
fig.update_yaxes(title_text='Value A')
401-
402-
fig.show()
403-
```
404-
405-
### Set axis title position
406-
407-
This example sets `standoff` attribute to cartesian axes to determine the distance between the tick labels and the axis title. Note that the axis title position is always constrained within the margins, so the actual standoff distance is always less than the set or default value. By default [automargin](https://plotly.com/python/setting-graph-size/#automatically-adjust-margins) is `True` in Plotly template for the cartesian axis, so the margins will be pushed to fit the axis title at given standoff distance.
408-
409-
```python
410-
import plotly.graph_objects as go
411-
412-
fig = go.Figure(go.Scatter(
413-
mode = "lines+markers",
414-
y = [4, 1, 3],
415-
x = ["December", "January", "February"]))
416-
417-
fig.update_layout(
418-
xaxis = dict(
419-
tickangle = 90,
420-
title_text = "Month",
421-
title_font = {"size": 20},
422-
title_standoff = 25),
423-
yaxis = dict(
424-
title_text = "Temperature",
425-
title_standoff = 25))
426-
427-
fig.show()
428-
```
429-
430-
##### Set axis title font
431-
432-
Here is an example that configures the font family, size, and color for the axis titles in a figure created using Plotly Express.
433-
434-
```python
435-
import plotly.express as px
436-
df = px.data.iris()
437-
438-
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
439-
fig.update_xaxes(title_font=dict(size=18, family='Courier', color='crimson'))
440-
fig.update_yaxes(title_font=dict(size=18, family='Courier', color='crimson'))
441-
442-
fig.show()
443-
```
444-
445445
#### Setting the Range of Axes Manually
446446

447447
The visible x and y axis range can be configured manually by setting the `range` axis property to a list of two values, the lower and upper boundary.
@@ -492,12 +492,12 @@ fig.add_trace(go.Scatter(
492492
fig.update_layout(
493493
width = 800,
494494
height = 500,
495-
title = "fixed-ratio axes",
496-
yaxis = dict(
497-
scaleanchor = "x",
498-
scaleratio = 1,
499-
)
495+
title = "fixed-ratio axes"
500496
)
497+
fig.update_yaxes(
498+
scaleanchor = "x",
499+
scaleratio = 1,
500+
)
501501

502502
fig.show()
503503
```
@@ -516,15 +516,15 @@ fig.add_trace(go.Scatter(
516516
fig.update_layout(
517517
width = 800,
518518
height = 500,
519-
title = "fixed-ratio axes with compressed axes",
520-
xaxis = dict(
521-
range=[-1,4], # sets the range of xaxis
522-
constrain="domain", # meanwhile compresses the xaxis by decreasing its "domain"
523-
),
524-
yaxis = dict(
525-
scaleanchor = "x",
526-
scaleratio = 1,
527-
),
519+
title = "fixed-ratio axes with compressed axes"
520+
)
521+
fig.update_xaxes(
522+
range=[-1,4], # sets the range of xaxis
523+
constrain="domain", # meanwhile compresses the xaxis by decreasing its "domain"
524+
)
525+
fig.update_yaxes(
526+
scaleanchor = "x",
527+
scaleratio = 1
528528
)
529529
fig.show()
530530
```
@@ -546,16 +546,17 @@ fig.add_trace(go.Scatter(
546546
fig.update_layout(
547547
width = 800,
548548
height = 500,
549-
title = "fixed-ratio axes",
550-
yaxis = dict(
551-
scaleanchor = "x",
552-
scaleratio = 1,
553-
),
554-
xaxis = dict(
555-
range=(-0.5, 3.5),
556-
constrain='domain'
557-
)
549+
title = "fixed-ratio axes"
550+
)
551+
fig.update_xaxes(
552+
scaleanchor = "x",
553+
scaleratio = 1,
558554
)
555+
fig.update_yaxes(
556+
range=(-0.5, 3.5),
557+
constrain='domain'
558+
)
559+
559560

560561
fig.show()
561562
```
@@ -577,15 +578,15 @@ fig.add_trace(go.Scatter(
577578
fig.update_layout(
578579
width = 800,
579580
height = 500,
580-
title = "fixed-ratio axes with compressed axes",
581-
xaxis = dict(
582-
range=[-1,4], # sets the range of xaxis
583-
constrain="domain", # meanwhile compresses the xaxis by decreasing its "domain"
584-
),
585-
yaxis = dict(
586-
scaleanchor = "x",
587-
scaleratio = 1,
588-
),
581+
title = "fixed-ratio axes with compressed axes"
582+
)
583+
fig.update_xaxes(
584+
range=[-1,4], # sets the range of xaxis
585+
constrain="domain", # meanwhile compresses the xaxis by decreasing its "domain"
586+
)
587+
fig.update_yaxes(
588+
scaleanchor = "x",
589+
scaleratio = 1,
589590
)
590591

591592
fig.show()

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fig.show()
107107

108108
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 (`=`).
109109

110-
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`.
110+
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`.
111111

112112
```python
113113
import plotly.express as px
@@ -117,9 +117,23 @@ fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
117117
fig.show()
118118
```
119119

120+
### Controlling Facet Ordering
121+
122+
By default, Plotly Express lays out categorical data in the order in which it appears in the underlying data. Every 2-d cartesian Plotly Express function also includes a `category_orders` keyword argument which can be used to control [the order in which categorical axes are drawn](/python/categorical-axes/), but beyond that can also control [the order in which discrete colors appear in the legend](/python/discrete-color/), and the order in which facets are laid out.
123+
124+
```python
125+
import plotly.express as px
126+
df = px.data.tips()
127+
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group", facet_col="sex",
128+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
129+
"smoker": ["Yes", "No"],
130+
"sex": ["Male", "Female"]})
131+
fig.show()
132+
```
133+
120134
### Controlling Facet Spacing
121135

122-
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.
136+
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.
123137

124138
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.
125139

@@ -140,7 +154,7 @@ fig.show()
140154

141155
### Synchronizing axes in subplots with `matches`
142156

143-
Using `facet_col` from `plotly.express` let [zoom](https://help.plotly.com/zoom-pan-hover-controls/#step-3-zoom-in-and-zoom-out-autoscale-the-plot) and [pan](https://help.plotly.com/zoom-pan-hover-controls/#step-6-pan-along-axes) each facet to the same range implicitly. However, if the subplots are created with `make_subplots`, the axis needs to be updated with `matches` parameter to update all the subplots accordingly.
157+
Using `facet_col` from `plotly.express` let [zoom](https://help.plotly.com/zoom-pan-hover-controls/#step-3-zoom-in-and-zoom-out-autoscale-the-plot) and [pan](https://help.plotly.com/zoom-pan-hover-controls/#step-6-pan-along-axes) each facet to the same range implicitly. However, if the subplots are created with `make_subplots`, the axis needs to be updated with `matches` parameter to update all the subplots accordingly.
144158

145159
Zoom in one trace below, to see the other subplots zoomed to the same x-axis range. To pan all the subplots, click and drag from the center of x-axis to the side:
146160

0 commit comments

Comments
 (0)