|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.2' |
| 9 | + jupytext_version: 1.3.0 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | + language_info: |
| 15 | + codemirror_mode: |
| 16 | + name: ipython |
| 17 | + version: 3 |
| 18 | + file_extension: .py |
| 19 | + mimetype: text/x-python |
| 20 | + name: python |
| 21 | + nbconvert_exporter: python |
| 22 | + pygments_lexer: ipython3 |
| 23 | + version: 3.7.3 |
| 24 | + plotly: |
| 25 | + description: How to add annotated horizontal and vertical lines in Python. |
| 26 | + display_as: file_settings |
| 27 | + language: python |
| 28 | + layout: base |
| 29 | + name: Shapes |
| 30 | + order: 37 |
| 31 | + permalink: python/horizontal-vertical-shapes/ |
| 32 | + thumbnail: thumbnail/horizontal-vertical-shapes.jpg |
| 33 | +--- |
| 34 | + |
| 35 | +### Horizontal and Vertical Lines and Boxes (Autoshapes) in Plotly.py |
| 36 | + |
| 37 | +Horizontal and vertical lines and rectangles (autoshapes) that span an entire |
| 38 | +plot can be added via the `add_hline`, `add_vline`, `add_hrect`, and `add_vrect` |
| 39 | +methods of `plotly.graph_objects.Figure`. These shapes are fixed to the |
| 40 | +endpoints of one axis, regardless of the range of the plot, and fixed to data |
| 41 | +coordinates on the other axis. For example |
| 42 | + |
| 43 | + |
| 44 | +```python |
| 45 | +import plotly.express as px |
| 46 | + |
| 47 | +df = px.data.iris() |
| 48 | +fig = px.scatter(df, x="sepal_length", y="sepal_width") |
| 49 | + |
| 50 | +# Add a vertical line that spans the entire y axis |
| 51 | +# intersecting the x axis at x=5 |
| 52 | +fig.add_vline(x=5, line_color="red") |
| 53 | +# Add a horizontal line that spans the entire x axis |
| 54 | +# intersecting the y axis at y=3 |
| 55 | +fig.add_hline(y=3, line_color="blue") |
| 56 | +# Add a vertical rectangle that spans the entire y axis |
| 57 | +# intersecting the x axis at 5.5 and 6.5 |
| 58 | +fig.add_vrect(x0=5.5, x1=6.5, line_color="purple") |
| 59 | +# Add a horizontal rectangle that spans the entire x axis |
| 60 | +# intersecting the y axis at 2.5 and 4 |
| 61 | +fig.add_hrect(y0=2.5, y1=4, line_color="orange") |
| 62 | +# (try dragging the plot around) |
| 63 | +fig.show() |
| 64 | +``` |
| 65 | + |
| 66 | +#### Adding Autoshapes to Multiple Facets / Subplots |
| 67 | + |
| 68 | +The same line or box can be added to multiple facets by using the `'all'` |
| 69 | +keyword in the `row` and `col` arguments like with `Figure.add_shape`. For |
| 70 | +example |
| 71 | +```python |
| 72 | +import plotly.express as px |
| 73 | + |
| 74 | +df = px.data.tips() |
| 75 | +fig = px.scatter(df, x="total_bill", y="tip", facet_row="smoker", facet_col="sex") |
| 76 | +# Adds a vertical line to all facets |
| 77 | +fig.add_vline(x=30, row="all", col="all", line_color="purple") |
| 78 | +# Adds a horizontal line to all the rows of the second column |
| 79 | +fig.add_hline(y=6, row="all", col=2, line_color="yellow") |
| 80 | +# Adds a vertical rectangle to all the columns of the first row |
| 81 | +fig.add_vrect(x0=20, x1=40, row=1, col="all", line_color="green") |
| 82 | +fig.show() |
| 83 | +``` |
| 84 | +The default `row` and `col` values are `"all"` so |
| 85 | +`fig.add_vline(x=30, line_color="purple")` is equivalent |
| 86 | +to `fig.add_vline(x=30, row="all", col="all", line_color="purple")` in the above |
| 87 | +example. |
| 88 | + |
| 89 | +#### Adding Text Annotations to Autoshapes |
| 90 | + |
| 91 | +Text can be added to an autoshape using the `annotation` keyword. Using the |
| 92 | +above example: |
| 93 | +```python |
| 94 | +import plotly.express as px |
| 95 | +import plotly.graph_objects as go |
| 96 | + |
| 97 | +df = px.data.tips() |
| 98 | +fig = px.scatter(df, x="total_bill", y="tip", facet_row="smoker", facet_col="sex") |
| 99 | +# Add annotations anchored to the top right corner of the resulting lines |
| 100 | +fig.add_vline(x=30, line_color="purple", annotation=go.layout.Annotation(text="A")) |
| 101 | +# Another way to add annotations when we are only interested in specifying text |
| 102 | +fig.add_hline(y=6, row="all", col=2, line_color="yellow", annotation_text="B") |
| 103 | +# Specify the position of the resulting annotations |
| 104 | +fig.add_vrect( |
| 105 | + x0=20, |
| 106 | + x1=40, |
| 107 | + row=1, |
| 108 | + col="all", |
| 109 | + line_color="green", |
| 110 | + annotation_text="C", |
| 111 | + annotation_position="bottom inside left", |
| 112 | +) |
| 113 | +fig.show() |
| 114 | +``` |
| 115 | +Call `help` on any of the autoshape functions in the Python interpreter to learn |
| 116 | +more (e.g., `help(fig.add_vline)`). |
0 commit comments