Skip to content

Commit 08feef3

Browse files
committed
Add reactive plots example
1 parent 1df6178 commit 08feef3

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

apps/reactive_plots.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# /// script
2+
# requires-python = ">=3.9"
3+
# dependencies = [
4+
# "altair==5.4.1",
5+
# "marimo",
6+
# "vega-datasets==0.9.0",
7+
# ]
8+
# ///
9+
10+
import marimo
11+
12+
__generated_with = "0.8.19"
13+
app = marimo.App(width="full")
14+
15+
16+
@app.cell(hide_code=True)
17+
def __(mo):
18+
mo.md("""# Welcome to marimo!""")
19+
return
20+
21+
22+
@app.cell
23+
def __(bars, mo, scatter):
24+
chart = mo.ui.altair_chart(scatter & bars)
25+
chart
26+
return (chart,)
27+
28+
29+
@app.cell
30+
def __(chart, mo):
31+
(filtered_data := mo.ui.table(chart.value))
32+
return (filtered_data,)
33+
34+
35+
@app.cell
36+
def __(alt, filtered_data, mo):
37+
mo.stop(not len(filtered_data.value))
38+
mpg_hist = mo.ui.altair_chart(
39+
alt.Chart(filtered_data.value)
40+
.mark_bar()
41+
.encode(alt.X("Miles_per_Gallon:Q", bin=True), y="count()")
42+
)
43+
horsepower_hist = mo.ui.altair_chart(
44+
alt.Chart(filtered_data.value)
45+
.mark_bar()
46+
.encode(alt.X("Horsepower:Q", bin=True), y="count()")
47+
)
48+
mo.hstack([mpg_hist, horsepower_hist], justify="space-around", widths="equal")
49+
return horsepower_hist, mpg_hist
50+
51+
52+
@app.cell
53+
def __(alt, data):
54+
cars = data.cars()
55+
brush = alt.selection_interval()
56+
scatter = (
57+
alt.Chart(cars)
58+
.mark_point()
59+
.encode(
60+
x="Horsepower",
61+
y="Miles_per_Gallon",
62+
color="Origin",
63+
)
64+
.add_params(brush)
65+
)
66+
bars = (
67+
alt.Chart(cars)
68+
.mark_bar()
69+
.encode(y="Origin:N", color="Origin:N", x="count(Origin):Q")
70+
.transform_filter(brush)
71+
)
72+
return bars, brush, cars, scatter
73+
74+
75+
@app.cell
76+
def __():
77+
import altair as alt
78+
from vega_datasets import data
79+
return alt, data
80+
81+
82+
@app.cell
83+
def __():
84+
import marimo as mo
85+
return (mo,)
86+
87+
88+
if __name__ == "__main__":
89+
app.run()

0 commit comments

Comments
 (0)