Skip to content

Commit 31d1d05

Browse files
authored
feat(plotly): add reactive line selection support (#8657)
1 parent 70bdbe4 commit 31d1d05

2 files changed

Lines changed: 136 additions & 1 deletion

File tree

docs/api/plotting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ alt.data_transformers.enable('marimo_csv')
105105
!!! warning "Supported charts"
106106

107107
marimo can render any Plotly plot, but [`mo.ui.plotly`][marimo.ui.plotly] only
108-
supports reactive selections for scatter/scattergl plots, bar charts,
108+
supports reactive selections for scatter/scattergl plots, pure line charts, bar charts,
109109
histograms, heatmaps, treemaps, and sunburst charts. If you require other kinds of
110110
selection, please [file an issue](https://github.com/marimo-team/marimo/issues).
111111

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# /// script
2+
# requires-python = ">=3.9"
3+
# dependencies = [
4+
# "marimo",
5+
# "plotly==6.5.1",
6+
# ]
7+
# ///
8+
9+
import marimo
10+
11+
__generated_with = "0.20.2"
12+
app = marimo.App(width="medium")
13+
14+
15+
@app.cell
16+
def _():
17+
import marimo as mo
18+
import plotly.graph_objects as go
19+
20+
return go, mo
21+
22+
23+
@app.cell
24+
def _(mo):
25+
mo.md("""
26+
# Reactive Plotly Pure Line Selection
27+
28+
This example focuses on pure line traces (`mode="lines"`).
29+
30+
Try:
31+
- clicking a line point
32+
- box selection
33+
- lasso selection
34+
35+
Then inspect `value`, `indices`, and `ranges` in Python.
36+
""")
37+
return
38+
39+
40+
@app.cell
41+
def _(go, mo):
42+
mo.md("## Single Pure Line")
43+
44+
_fig_single = go.Figure(
45+
data=go.Scatter(
46+
x=[0, 1, 2, 3, 4, 5, 6],
47+
y=[0, 4, 2, 6, 3, 8, 5],
48+
mode="lines",
49+
name="line_a",
50+
line={"color": "#636EFA", "width": 2},
51+
)
52+
)
53+
_fig_single.update_layout(
54+
title="Click + Box/Lasso on Pure Line",
55+
xaxis_title="x",
56+
yaxis_title="y",
57+
clickmode="event+select",
58+
)
59+
60+
pure_line = mo.ui.plotly(_fig_single)
61+
pure_line
62+
return (pure_line,)
63+
64+
65+
@app.cell
66+
def _(mo, pure_line):
67+
mo.md(f"""
68+
### Single Line Selection Output
69+
70+
**Selected rows:** {len(pure_line.value)}
71+
72+
**Selected indices:** {pure_line.indices}
73+
74+
**Selection ranges:** {pure_line.ranges}
75+
76+
**Preview:**
77+
{pure_line.value[:10]}
78+
""")
79+
return
80+
81+
82+
@app.cell
83+
def _(go, mo):
84+
mo.md("## Multiple Pure Lines")
85+
86+
_fig_multi = go.Figure()
87+
_fig_multi.add_trace(
88+
go.Scatter(
89+
x=[1, 2, 3, 4, 5],
90+
y=[12, 15, 18, 14, 16],
91+
mode="lines",
92+
name="inside_box_line",
93+
line={"color": "#00CC96", "width": 2},
94+
)
95+
)
96+
_fig_multi.add_trace(
97+
go.Scatter(
98+
x=[1, 2, 3, 4, 5],
99+
y=[50, 60, 70, 65, 55],
100+
mode="lines",
101+
name="outside_box_line",
102+
line={"color": "#EF553B", "width": 2},
103+
)
104+
)
105+
_fig_multi.update_layout(
106+
title="XY Filtering Across Multiple Pure Lines",
107+
xaxis_title="x",
108+
yaxis_title="y",
109+
clickmode="event+select",
110+
)
111+
112+
multi_line = mo.ui.plotly(_fig_multi)
113+
multi_line
114+
return (multi_line,)
115+
116+
117+
@app.cell
118+
def _(mo, multi_line):
119+
mo.md(f"""
120+
### Multi-Line Selection Output
121+
122+
**Selected rows:** {len(multi_line.value)}
123+
124+
**Selected indices:** {multi_line.indices}
125+
126+
**Selection ranges:** {multi_line.ranges}
127+
128+
**Preview:**
129+
{multi_line.value[:10]}
130+
""")
131+
return
132+
133+
134+
if __name__ == "__main__":
135+
app.run()

0 commit comments

Comments
 (0)