Skip to content

Commit 8808e8b

Browse files
more shape/text doc cleanup
1 parent 870c75b commit 8808e8b

File tree

3 files changed

+257
-604
lines changed

3 files changed

+257
-604
lines changed

doc/python/horizontal-vertical-shapes.md

Lines changed: 66 additions & 58 deletions
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.0
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.3
23+
version: 3.7.7
2424
plotly:
2525
description: How to add annotated horizontal and vertical lines in Python.
2626
display_as: file_settings
@@ -32,11 +32,11 @@ jupyter:
3232
thumbnail: thumbnail/shape.jpg
3333
---
3434

35-
### Horizontal and Vertical Lines and Rectangles (Autoshapes)
35+
### Horizontal and Vertical Lines and Rectangles
3636

3737
*introduced in plotly 4.12*
3838

39-
Horizontal and vertical lines and rectangles (autoshapes) that span an entire
39+
Horizontal and vertical lines and rectangles that span an entire
4040
plot can be added via the `add_hline`, `add_vline`, `add_hrect`, and `add_vrect`
4141
methods of `plotly.graph_objects.Figure`. Shapes added with these methods are
4242
added as [layout shapes](/python/shapes) (as shown when doing `print(fig)`, for
@@ -45,77 +45,85 @@ range of the plot, and fixed to data coordinates on the other axis. The
4545
following shows some possibilities, try panning and zooming the resulting figure
4646
to see how the shapes stick to some axes:
4747

48+
```python
49+
import plotly.express as px
50+
51+
df = px.data.iris()
52+
fig = px.scatter(df, x="petal_length", y="petal_width")
53+
fig.add_hline(y=0.9)
54+
fig.add_vrect(x0=0.9, x1=2)
55+
fig.show()
56+
```
57+
58+
These shapes can be styled by passing the same arguments as are accepted by `add_shape`:
4859

4960
```python
5061
import plotly.express as px
5162

5263
df = px.data.iris()
53-
fig = px.scatter(df, x="sepal_length", y="sepal_width")
54-
55-
# Add a vertical line that spans the entire y axis
56-
# intersecting the x axis at x=5
57-
fig.add_vline(x=5, line_color="red")
58-
# Add a horizontal line that spans the entire x axis
59-
# intersecting the y axis at y=3
60-
fig.add_hline(y=3, line_color="blue")
61-
# Add a vertical rectangle that spans the entire y axis
62-
# intersecting the x axis at 5.5 and 6.5
63-
fig.add_vrect(x0=5.5, x1=6.5, line_color="purple")
64-
# Add a horizontal rectangle that spans the entire x axis
65-
# intersecting the y axis at 2.5 and 4
66-
fig.add_hrect(y0=2.5, y1=4, line_color="orange")
67-
# (try panning and zooming the plot)
64+
fig = px.scatter(df, x="petal_length", y="petal_width")
65+
fig.add_vline(x=2.5, line_width=3, line_dash="dash", line_color="green")
66+
fig.add_hrect(y0=0.9, y1=2.6, line_width=0, fillcolor="red", opacity=0.2)
67+
fig.show()
68+
```
69+
70+
#### Adding Text Annotations
71+
72+
[Text annotations](/python/text-and-annotations) can optionally be added to an autoshape
73+
using the `annotation_text` keyword argument, and positioned using the `annotation_position` argument:
74+
75+
```python
76+
import plotly.express as px
77+
78+
df = px.data.stocks(indexed=True)
79+
fig = px.line(df)
80+
fig.add_hline(y=1, line_dash="dot",
81+
annotation_text="Jan 1, 2018 baseline",
82+
annotation_position="bottom right")
83+
fig.add_vrect(x0="2018-09-24", x1="2018-12-18",
84+
annotation_text="decline", annotation_position="top left",
85+
fillcolor="green", opacity=0.25, line_width=0)
6886
fig.show()
6987
```
7088

71-
#### Adding Autoshapes to Multiple Facets / Subplots
89+
Extra formatting of the annotation can be done using magic-underscores prefixed by `annotation_` or by passing a `dict` or `go.layout.Annotation` instance to the `annotation` argument:
7290

73-
The same line or box can be added to multiple facets by using the `'all'`
74-
keyword in the `row` and `col` arguments like with `Figure.add_shape`. For
75-
example
7691
```python
7792
import plotly.express as px
7893

79-
df = px.data.tips()
80-
fig = px.scatter(df, x="total_bill", y="tip", facet_row="smoker", facet_col="sex")
81-
# Adds a vertical line to all facets
82-
fig.add_vline(x=30, row="all", col="all", line_color="purple")
83-
# Adds a horizontal line to all the rows of the second column
84-
fig.add_hline(y=6, row="all", col=2, line_color="yellow")
85-
# Adds a vertical rectangle to all the columns of the first row
86-
fig.add_vrect(x0=20, x1=40, row=1, col="all", line_color="green")
94+
df = px.data.stocks(indexed=True)
95+
fig = px.line(df)
96+
fig.add_hline(y=1, line_dash="dot",
97+
annotation_text="Jan 1, 2018 baseline",
98+
annotation_position="bottom right",
99+
annotation_font_size=20,
100+
annotation_font_color="blue"
101+
)
102+
fig.add_vrect(x0="2018-09-24", x1="2018-12-18",
103+
annotation_text="decline", annotation_position="top left",
104+
annotation=dict(font_size=20, font_family="Times New Roman"),
105+
fillcolor="green", opacity=0.25, line_width=0)
87106
fig.show()
88107
```
89-
The default `row` and `col` values are `"all"` so
90-
`fig.add_vline(x=30, line_color="purple")` is equivalent
91-
to `fig.add_vline(x=30, row="all", col="all", line_color="purple")` in the above
92-
example.
93108

94-
#### Adding Text Annotations to Autoshapes
109+
#### Adding to Multiple Facets / Subplots
95110

96-
Text [annotations](/python/text-and-annotations) can be added to an autoshape
97-
using the `annotation` keyword. Using the above example:
111+
The same line or box can be added to multiple [subplots](/python/subplots/) or [facets](/python/facet-plots/) by setting the `row` and/or `col` to `"all"`. The default `row` and `col` values are `"all"`.
98112
```python
99113
import plotly.express as px
100-
import plotly.graph_objects as go
101-
102-
df = px.data.tips()
103-
fig = px.scatter(df, x="total_bill", y="tip", facet_row="smoker", facet_col="sex")
104-
# Add annotations anchored to the top right corner of the resulting lines
105-
fig.add_vline(x=30, line_color="purple", annotation=go.layout.Annotation(text="A"))
106-
# Another way to add annotations when we are only interested in specifying text
107-
fig.add_hline(y=6, row="all", col=2, line_color="yellow", annotation_text="B")
108-
# Specify the position of the resulting annotations
109-
fig.add_vrect(
110-
x0=20,
111-
x1=40,
112-
row=1,
113-
col="all",
114-
line_color="green",
115-
annotation_text="C",
116-
annotation_position="bottom inside left",
117-
)
114+
115+
df = px.data.stocks(indexed=True)
116+
fig = px.line(df, facet_col="company", facet_col_wrap=2)
117+
fig.add_hline(y=1, line_dash="dot", row=3, col="all",
118+
annotation_text="Jan 1, 2018 baseline",
119+
annotation_position="bottom right")
120+
fig.add_vrect(x0="2018-09-24", x1="2018-12-18", row="all", col=1,
121+
annotation_text="decline", annotation_position="top left",
122+
fillcolor="green", opacity=0.25, line_width=0)
118123
fig.show()
119124
```
120-
Call `help` on any of the autoshape functions in the Python interpreter to learn
121-
more (e.g., `help(fig.add_vline)`).
125+
### Reference
126+
127+
More details are available about [layout shapes](/python/shapes/) and [annotations](/python/text-and-annotations).
128+
129+
Reference documentation is also available for [`add_hline`](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html?highlight=add_hline#plotly.graph_objects.Figure.add_hline), [`add_vline`](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html?highlight=add_vline#plotly.graph_objects.Figure.add_vline), [`add_hrect`](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html?highlight=add_hrect#plotly.graph_objects.Figure.add_hrect), [`add_vrect`](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html?highlight=add_vrect#plotly.graph_objects.Figure.add_vrect).

0 commit comments

Comments
 (0)