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
[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!")
445
464
fig.show()
446
465
```
447
466
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!")
### Legend items for continuous fields (2D and 3D)
449
522
450
523
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