Skip to content

Commit e88a446

Browse files
Merge pull request #3666 from plotly/legend-example
Legend example for `groupclick`
2 parents 3644568 + f950aa8 commit e88a446

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

Diff for: doc/python/legend.md

+58-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
@@ -445,6 +445,60 @@ fig.update_layout(title="Try Clicking on the Legend Items!")
445445
fig.show()
446446
```
447447

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!")
496+
fig.update_layout(legend=dict(groupclick="toggleitem"))
497+
498+
fig.show()
499+
500+
```
501+
448502
### Legend items for continuous fields (2D and 3D)
449503

450504
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)