You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/python/legend.md
+58-4
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,10 @@ jupyter:
5
5
text_representation:
6
6
extension: .md
7
7
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
10
10
kernelspec:
11
-
display_name: Python 3
11
+
display_name: Python 3 (ipykernel)
12
12
language: python
13
13
name: python3
14
14
language_info:
@@ -20,7 +20,7 @@ jupyter:
20
20
name: python
21
21
nbconvert_exporter: python
22
22
pygments_lexer: ipython3
23
-
version: 3.7.7
23
+
version: 3.9.0
24
24
plotly:
25
25
description: How to configure and style the legend in Plotly with Python.
26
26
display_as: file_settings
@@ -445,6 +445,60 @@ fig.update_layout(title="Try Clicking on the Legend Items!")
445
445
fig.show()
446
446
```
447
447
448
+
#### Group click toggle behavior
449
+
450
+
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 toggles the visibility of all items in the same group as the item clicked on.
451
+
452
+
```python
453
+
import plotly.graph_objects as go
454
+
455
+
fig = go.Figure()
456
+
457
+
fig.add_trace(go.Scatter(
458
+
x=[1, 2, 3],
459
+
y=[2, 1, 3],
460
+
legendgroup="group", # this can be any string, not just "group"
461
+
legendgrouptitle_text="First Group Title",
462
+
name="first legend group",
463
+
mode="markers",
464
+
marker=dict(color="Crimson", size=10)
465
+
))
466
+
467
+
fig.add_trace(go.Scatter(
468
+
x=[1, 2, 3],
469
+
y=[2, 2, 2],
470
+
legendgroup="group",
471
+
name="first legend group - average",
472
+
mode="lines",
473
+
line=dict(color="Crimson")
474
+
))
475
+
476
+
fig.add_trace(go.Scatter(
477
+
x=[1, 2, 3],
478
+
y=[4, 9, 2],
479
+
legendgroup="group2",
480
+
legendgrouptitle_text="Second Group Title",
481
+
name="second legend group",
482
+
mode="markers",
483
+
marker=dict(color="MediumPurple", size=10)
484
+
))
485
+
486
+
fig.add_trace(go.Scatter(
487
+
x=[1, 2, 3],
488
+
y=[5, 5, 5],
489
+
legendgroup="group2",
490
+
name="second legend group - average",
491
+
mode="lines",
492
+
line=dict(color="MediumPurple")
493
+
))
494
+
495
+
fig.update_layout(title="Try Clicking on the Legend Items!")
### Legend items for continuous fields (2D and 3D)
449
503
450
504
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