Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

reactive plots example

Akshay Agrawala39c19987385
diff --git a/examples/reactive_plots.py b/examples/reactive_plots.py
new file mode 100644
index 0000000000..a02fce39bf
--- /dev/null
+++ b/examples/reactive_plots.py
@@ -0,0 +1,76 @@
+import marimo
+
+__generated_with = "0.1.28"
+app = marimo.App(width="full")
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __(mo):
+ mo.md("# Reactive plots! 🚗 ⚡")
+ return
+
+
+def __(alt, data, mo):
+ cars = data.cars()
+
+ brush = alt.selection_interval()
+ scatter = (
+ alt.Chart(cars)
+ .mark_point()
+ .encode(
+ x="Horsepower",
+ y="Miles_per_Gallon",
+ color="Origin",
+ )
+ .add_params(brush)
+ )
+ bars = (
+ alt.Chart(cars)
+ .mark_bar()
+ .encode(y="Origin:N", color="Origin:N", x="count(Origin):Q")
+ .transform_filter(brush)
+ )
+ chart = mo.ui.altair_chart(scatter & bars)
+ chart
+ return bars, brush, cars, chart, scatter
+
+
+def __(chart, mo):
+ (filtered_data := mo.ui.table(chart.value))
+ return filtered_data,
+
+
+def __(alt, filtered_data, mo):
+ mo.stop(not len(filtered_data.value))
+ mpg_hist = mo.ui.altair_chart(
+ alt.Chart(filtered_data.value)
+ .mark_bar()
+ .encode(alt.X("Miles_per_Gallon:Q", bin=True), y="count()")
+ )
+ horsepower_hist = mo.ui.altair_chart(
+ alt.Chart(filtered_data.value)
+ .mark_bar()
+ .encode(alt.X("Horsepower:Q", bin=True), y="count()")
+ )
+ mo.hstack([mpg_hist, horsepower_hist], justify="space-around", widths="equal")
+ return horsepower_hist, mpg_hist
+
+
+def __():
+ import altair as alt
+ from vega_datasets import data
+ return alt, data
+
+
+if __name__ == "__main__":
+ app.run()