-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
add_{hline,vline,hrect,vrect}
and add_{shape,image,annotation}
to multiple facets, squashed
#2840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ad54177
a7d95be
6a86381
be7eb0f
0c5126c
00eca47
9cc5777
e2f18b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,116 @@ | ||||||
--- | ||||||
jupyter: | ||||||
jupytext: | ||||||
notebook_metadata_filter: all | ||||||
text_representation: | ||||||
extension: .md | ||||||
format_name: markdown | ||||||
format_version: '1.2' | ||||||
jupytext_version: 1.3.0 | ||||||
kernelspec: | ||||||
display_name: Python 3 | ||||||
language: python | ||||||
name: python3 | ||||||
language_info: | ||||||
codemirror_mode: | ||||||
name: ipython | ||||||
version: 3 | ||||||
file_extension: .py | ||||||
mimetype: text/x-python | ||||||
name: python | ||||||
nbconvert_exporter: python | ||||||
pygments_lexer: ipython3 | ||||||
version: 3.7.3 | ||||||
plotly: | ||||||
description: How to add annotated horizontal and vertical lines in Python. | ||||||
display_as: file_settings | ||||||
language: python | ||||||
layout: base | ||||||
name: Shapes | ||||||
order: 37 | ||||||
permalink: python/horizontal-vertical-shapes/ | ||||||
thumbnail: thumbnail/horizontal-vertical-shapes.jpg | ||||||
--- | ||||||
|
||||||
### Horizontal and Vertical Lines and Boxes (Autoshapes) in Plotly.py | ||||||
|
||||||
Horizontal and vertical lines and rectangles (autoshapes) that span an entire | ||||||
plot can be added via the `add_hline`, `add_vline`, `add_hrect`, and `add_vrect` | ||||||
methods of `plotly.graph_objects.Figure`. These shapes are fixed to the | ||||||
endpoints of one axis, regardless of the range of the plot, and fixed to data | ||||||
coordinates on the other axis. For example | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
```python | ||||||
import plotly.express as px | ||||||
|
||||||
df = px.data.iris() | ||||||
fig = px.scatter(df, x="sepal_length", y="sepal_width") | ||||||
|
||||||
# Add a vertical line that spans the entire y axis | ||||||
# intersecting the x axis at x=5 | ||||||
fig.add_vline(x=5, line_color="red") | ||||||
# Add a horizontal line that spans the entire x axis | ||||||
# intersecting the y axis at y=3 | ||||||
fig.add_hline(y=3, line_color="blue") | ||||||
# Add a vertical rectangle that spans the entire y axis | ||||||
# intersecting the x axis at 5.5 and 6.5 | ||||||
fig.add_vrect(x0=5.5, x1=6.5, line_color="purple") | ||||||
# Add a horizontal rectangle that spans the entire x axis | ||||||
# intersecting the y axis at 2.5 and 4 | ||||||
fig.add_hrect(y0=2.5, y1=4, line_color="orange") | ||||||
# (try dragging the plot around) | ||||||
fig.show() | ||||||
``` | ||||||
|
||||||
#### Adding Autoshapes to Multiple Facets / Subplots | ||||||
|
||||||
The same line or box can be added to multiple facets by using the `'all'` | ||||||
keyword in the `row` and `col` arguments like with `Figure.add_shape`. For | ||||||
example | ||||||
```python | ||||||
import plotly.express as px | ||||||
|
||||||
df = px.data.tips() | ||||||
fig = px.scatter(df, x="total_bill", y="tip", facet_row="smoker", facet_col="sex") | ||||||
# Adds a vertical line to all facets | ||||||
fig.add_vline(x=30, row="all", col="all", line_color="purple") | ||||||
# Adds a horizontal line to all the rows of the second column | ||||||
fig.add_hline(y=6, row="all", col=2, line_color="yellow") | ||||||
# Adds a vertical rectangle to all the columns of the first row | ||||||
fig.add_vrect(x0=20, x1=40, row=1, col="all", line_color="green") | ||||||
fig.show() | ||||||
``` | ||||||
The default `row` and `col` values are `"all"` so | ||||||
`fig.add_vline(x=30, line_color="purple")` is equivalent | ||||||
to `fig.add_vline(x=30, row="all", col="all", line_color="purple")` in the above | ||||||
example. | ||||||
|
||||||
#### Adding Text Annotations to Autoshapes | ||||||
|
||||||
Text can be added to an autoshape using the `annotation` keyword. Using the | ||||||
above example: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please add a link to the annotations tutorial? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, sure! Is this how you do it? |
||||||
```python | ||||||
import plotly.express as px | ||||||
import plotly.graph_objects as go | ||||||
|
||||||
df = px.data.tips() | ||||||
fig = px.scatter(df, x="total_bill", y="tip", facet_row="smoker", facet_col="sex") | ||||||
# Add annotations anchored to the top right corner of the resulting lines | ||||||
fig.add_vline(x=30, line_color="purple", annotation=go.layout.Annotation(text="A")) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we discourage using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah let's use a dict here please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, this was one of the specifications, that it should accept a |
||||||
# Another way to add annotations when we are only interested in specifying text | ||||||
fig.add_hline(y=6, row="all", col=2, line_color="yellow", annotation_text="B") | ||||||
# Specify the position of the resulting annotations | ||||||
fig.add_vrect( | ||||||
x0=20, | ||||||
x1=40, | ||||||
row=1, | ||||||
col="all", | ||||||
line_color="green", | ||||||
annotation_text="C", | ||||||
annotation_position="bottom inside left", | ||||||
) | ||||||
fig.show() | ||||||
``` | ||||||
Call `help` on any of the autoshape functions in the Python interpreter to learn | ||||||
more (e.g., `help(fig.add_vline)`). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,15 +309,15 @@ fig.show(config={'doubleClick': 'reset'}) | |
|
||
_introduced in plotly 4.7_ | ||
|
||
It can be useful to add shapes to a layout image, for highlighting an object, drawing bounding boxes as part of a machine learning training set, or identifying seeds for a segmentation algorithm. | ||
It can be useful to add shapes to a layout image, for highlighting an object, drawing bounding boxes as part of a machine learning training set, or identifying seeds for a segmentation algorithm. | ||
|
||
In order to enable shape drawing, you need to | ||
- define a dragmode corresponding to a drawing tool (`'drawline'`,`'drawopenpath'`, `'drawclosedpath'`, `'drawcircle'`, or `'drawrect'`) | ||
- add [modebar buttons](/python/configuration-options#add-optional-shapedrawing-buttons-to-modebar) corresponding to the drawing tools you wish to use. | ||
|
||
The style of new shapes is specified by the `newshape` layout attribute. Shapes can be selected and modified after they have been drawn. More details and examples are given in the [tutorial on shapes](/python/shapes#drawing-shapes-on-cartesian-plots). | ||
|
||
Drawing or modifying a shape triggers a `relayout` event, which [can be captured by a callback inside a Dash application](https://dash.plotly.com/interactive-graphing). | ||
Drawing or modifying a shape triggers a `relayout` event, which [can be captured by a callback inside a Dash application](https://dash.plotly.com/interactive-graphing). | ||
|
||
```python | ||
import plotly.graph_objects as go | ||
|
@@ -339,7 +339,7 @@ fig.add_layout_image( | |
) | ||
fig.update_xaxes(showgrid=False, range=(0, img_width)) | ||
fig.update_yaxes(showgrid=False, scaleanchor='x', range=(img_height, 0)) | ||
# Line shape added programatically | ||
# Line shape added programatically | ||
fig.add_shape( | ||
type='line', xref='x', yref='y', | ||
x0=650, x1=1080, y0=380, y1=180, line_color='cyan' | ||
|
@@ -359,5 +359,8 @@ fig.show(config={'modeBarButtonsToAdd':['drawline', | |
]}) | ||
``` | ||
|
||
|
||
### Images Placed Relative to the Axes | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. placeholder for example here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, woops ;) |
||
#### Reference | ||
See https://plotly.com/python/reference/layout/images/ for more information and chart attribute options! | ||
See https://plotly.com/python/reference/layout/images/ for more information and chart attribute options! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe also mention that users can pan and zoom, the shapes will adapt