Skip to content

Commit 5fed9bc

Browse files
committed
Update legend.md
1 parent 3644568 commit 5fed9bc

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

doc/python/legend.md

+77-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.2'
9-
jupytext_version: 1.4.2
8+
format_version: '1.3'
9+
jupytext_version: 1.13.7
1010
kernelspec:
11-
display_name: Python 3
11+
display_name: Python 3 (ipykernel)
1212
language: python
1313
name: python3
1414
language_info:
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.7.7
23+
version: 3.9.0
2424
plotly:
2525
description: How to configure and style the legend in Plotly with Python.
2626
display_as: file_settings
@@ -150,6 +150,25 @@ fig.update_layout(legend=dict(
150150
fig.show()
151151
```
152152

153+
### Legend Groups
154+
155+
```python
156+
import plotly.express as px
157+
158+
df = px.data.gapminder().query("year==2007")
159+
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent",
160+
size="pop", size_max=45, log_x=True)
161+
162+
fig.update_layout(legend=dict(
163+
yanchor="top",
164+
y=0.99,
165+
xanchor="left",
166+
x=0.01
167+
))
168+
169+
fig.show()
170+
```
171+
153172
#### Legends in Dash
154173

155174
[Dash](https://plotly.com/dash/) is the best way to build analytical apps in Python using Plotly figures. To run the app below, run `pip install dash`, click "Download" to get the code and run `python app.py`.
@@ -445,6 +464,60 @@ fig.update_layout(title="Try Clicking on the Legend Items!")
445464
fig.show()
446465
```
447466

467+
#### Group click toggle behavior
468+
469+
You can also define the toggle behavior for when a user clicks an item in a group. Here we set the `groupclick` for the `legend` to `toggleitem`. This toggles the visibility of just the item clicked on by the user. Set to `togglegroup` and it togges the visibility of all items in the same group as the item clicked on.
470+
471+
```python
472+
import plotly.graph_objects as go
473+
474+
fig = go.Figure()
475+
476+
fig.add_trace(go.Scatter(
477+
x=[1, 2, 3],
478+
y=[2, 1, 3],
479+
legendgroup="group", # this can be any string, not just "group"
480+
legendgrouptitle_text="First Group Title",
481+
name="first legend group",
482+
mode="markers",
483+
marker=dict(color="Crimson", size=10)
484+
))
485+
486+
fig.add_trace(go.Scatter(
487+
x=[1, 2, 3],
488+
y=[2, 2, 2],
489+
legendgroup="group",
490+
name="first legend group - average",
491+
mode="lines",
492+
line=dict(color="Crimson")
493+
))
494+
495+
fig.add_trace(go.Scatter(
496+
x=[1, 2, 3],
497+
y=[4, 9, 2],
498+
legendgroup="group2",
499+
legendgrouptitle_text="Second Group Title",
500+
name="second legend group",
501+
mode="markers",
502+
marker=dict(color="MediumPurple", size=10)
503+
))
504+
505+
fig.add_trace(go.Scatter(
506+
x=[1, 2, 3],
507+
y=[5, 5, 5],
508+
legendgroup="group2",
509+
name="second legend group - average",
510+
mode="lines",
511+
line=dict(color="MediumPurple")
512+
))
513+
514+
fig.update_layout(title="Try Clicking on the Legend Items!")
515+
fig.update_layout(legend=dict(groupclick="toggleitem"))
516+
517+
fig.show()
518+
519+
```
520+
448521
### Legend items for continuous fields (2D and 3D)
449522

450523
Traces corresponding to 2D fields (e.g. `go.Heatmap`, `go.Histogram2d`) or 3D fields (e.g. `go.Isosurface`, `go.Volume`, `go.Cone`) can also appear in the legend. They come with legend icons corresponding to each trace type, which are colored using the same colorscale as the trace.

0 commit comments

Comments
 (0)