Skip to main content
Make reactive charts with Altair.

Usage

import altair as alt
import marimo as mo
from vega_datasets import data

chart = (
    alt.Chart(data.cars())
    .mark_point()
    .encode(
        x="Horsepower",
        y="Miles_per_Gallon",
        color="Origin",
    )
)

chart = mo.ui.altair_chart(chart)
# View the chart and selected data as a dataframe
mo.hstack([chart, chart.value])

Signature

mo.ui.altair_chart(
    chart: altair.Chart,
    chart_selection: Literal["point"] | Literal["interval"] | bool = True,
    legend_selection: list[str] | bool = True,
    *,
    label: str = "",
    on_change: Optional[Callable[[ChartDataType], None]] = None
)

Parameters

chart
altair.Chart
required
An Altair Chart object.
chart_selection
Literal['point'] | Literal['interval'] | bool
default:"True"
Selection type: "point", "interval", or a bool. Defaults to True which will automatically detect the best selection type. This is ignored if the chart already has a point/interval selection param.
legend_selection
list[str] | bool
default:"True"
List of legend fields (columns) for which to enable selection, True to enable selection for all fields, or False to disable selection entirely. This is ignored if the chart already has a legend selection param.
label
str
default:""
Markdown label for the element.
on_change
Optional[Callable[[ChartDataType], None]]
default:"None"
Optional callback to run when this element’s value changes.

Attributes

value
ChartDataType
A dataframe of the plot data filtered by the selections.
dataframe
ChartDataType
A dataframe of the unfiltered chart data.
selections
ChartSelection
The selection of the chart; this may be an interval along the name of an axis or a selection of points.

Methods

apply_selection

Apply the selection to a DataFrame. This method is useful when you have a layered chart and you want to apply the selection to a DataFrame.
selected_df = chart.apply_selection(cars)
Parameters:
  • df (ChartDataType): A DataFrame to apply the selection to.
Returns: ChartDataType - A DataFrame of the plot data filtered by the selections.

Build docs developers (and LLMs) love