|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.3' |
| 9 | + jupytext_version: 1.13.7 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 (ipykernel) |
| 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.9.0 |
| 24 | + plotly: |
| 25 | + description: How to use selections in Python. Examples of adding and styling selections. |
| 26 | + display_as: file_settings |
| 27 | + language: python |
| 28 | + layout: base |
| 29 | + name: Selections |
| 30 | + order: 26 |
| 31 | + permalink: python/selections/ |
| 32 | + thumbnail: thumbnail/make_selection.png |
| 33 | +--- |
| 34 | + |
| 35 | +## Adding Selections |
| 36 | + |
| 37 | +*New in 5.10* |
| 38 | + |
| 39 | +You can add persistent selections to a rendered figure using the **Box Select** and **Lasso Select** tools in the mode bar. |
| 40 | +To add multiple selections, select **Shift** when making new selections. |
| 41 | +To clear a selection, double-click it. You can clear all selections by double-clicking any unselected area of the figure. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +You can also add selections to a figure that displays when it renders using `fig.add_selection`. |
| 46 | +Here, we add a rectangular selection that connects the points `3.0`, `6.5`, `3.5`, and `5.5`. |
| 47 | + |
| 48 | + |
| 49 | +```python |
| 50 | +import plotly.express as px |
| 51 | + |
| 52 | +df = px.data.iris() |
| 53 | + |
| 54 | +fig = px.scatter(df, x="sepal_width", y="sepal_length") |
| 55 | +fig.add_selection(type="rect", x0=3.0, y0=6.5, x1=3.5, y1=5.5) |
| 56 | + |
| 57 | +fig.show() |
| 58 | +``` |
| 59 | + |
| 60 | +## Styling Selections |
| 61 | + |
| 62 | + |
| 63 | +In the above example, we added a selection to the figure that is displayed when the figure renders. |
| 64 | +`fig.add_selection` accepts additional properties that you can use to style the selection. Here, we add a `color`, `width`, and specify the `dash` type for the selection. |
| 65 | + |
| 66 | + |
| 67 | +```python |
| 68 | +import plotly.express as px |
| 69 | + |
| 70 | +df = px.data.iris() |
| 71 | + |
| 72 | +fig = px.scatter(df, x="sepal_width", y="sepal_length") |
| 73 | +fig.add_selection(type="rect", |
| 74 | + x0=2.5, y0=6.5, x1=3.5, y1=5.5, |
| 75 | + line=dict( |
| 76 | + color="Crimson", |
| 77 | + width=2, |
| 78 | + dash="dash", |
| 79 | + )) |
| 80 | + |
| 81 | +fig.show() |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +## Fill Color for Active Selections |
| 86 | + |
| 87 | +You can style active selections with `activeselection`. In this example, we set active selections to appear with a `fillcolor` of `blue`. |
| 88 | + |
| 89 | +```python |
| 90 | +import plotly.express as px |
| 91 | + |
| 92 | +df = px.data.iris() |
| 93 | + |
| 94 | +fig = px.scatter(df, x="sepal_width", y="sepal_length") |
| 95 | +fig.add_selection(type="rect", x0=3.0, y0=6.5, x1=3.5, y1=5.5) |
| 96 | + |
| 97 | +fig.update_layout(dragmode='select', |
| 98 | + activeselection=dict(fillcolor='blue')) |
| 99 | + |
| 100 | +fig.show() |
| 101 | +``` |
| 102 | + |
| 103 | +## Styling New Selections |
| 104 | + |
| 105 | +You can style new selections made on the figure by setting properties on `newselection`. |
| 106 | +Try making a new selection on the figure to try it out. |
| 107 | + |
| 108 | +```python |
| 109 | +import plotly.express as px |
| 110 | + |
| 111 | +df = px.data.iris() |
| 112 | + |
| 113 | +fig = px.scatter(df, x="sepal_width", y="sepal_length") |
| 114 | + |
| 115 | +fig.update_layout(dragmode='select', |
| 116 | + newselection=dict(line=dict(color='blue'))) |
| 117 | + |
| 118 | +fig.show() |
| 119 | +``` |
| 120 | + |
| 121 | +## More on Selections |
| 122 | + |
| 123 | +For more on selections, see the [selections section of the `dcc.Graph` page](https://dash.plotly.com/dash-core-components/graph#selections) in the Dash docs. |
0 commit comments