Plotting#
marimo supports most major plotting libraries, including Matplotlib, Seaborn, Plotly, and Altair. Just import your plotting library of choice and use it as you normally would.
For more information about plotting, see the plotting guide.
Reactive charts with Altair#
- marimo.ui.altair_chart(chart: altair.Chart, chart_selection: Literal['point'] | Literal['interval'] | bool = True, legend_selection: list[str] | bool = True, *, label: str = '', on_change: Callable[[pd.DataFrame], None] | None = None) None#
Make reactive charts with Altair
Use
mo.ui.altair_chartto make Altair charts reactive: select chart data with your cursor on the frontend, get them as a Pandas dataframe in Python!For Polars DataFrames, you can convert to a Pandas DataFrame. However the returned DataFrame will still be a Pandas DataFrame, so you will need to convert back to a Polars DataFrame if you want.
Example.
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])
Attributes.
value: a Pandas dataframe of the plot data filtered by the selectionsdataframe: a Pandas dataframe of the unfiltered chart dataselections: the selection of the chart; this may be an interval along the name of an axis or a selection of points
Initialization Args.
chart: Analtair.Chartchart_selection: optional selection type,"point","interval", or a bool; defaults toTruewhich will automatically detect the best selection typelegend_selection: optional list of legend fields (columns) for which to enable selection,Trueto enable selection for all fields, orFalseto disable selection entirelylabel: optional text label for the elementon_change: optional callback to run when this element’s value changes
Reactive plots with Plotly#
- marimo.ui.plotly(figure: go.Figure, *, label: str = '', on_change: Callable[[JSONType], None] | None = None) None#
Make reactive plots with Plotly.
Use
mo.ui.plotlyto make plotly plots reactive: select data with your cursor on the frontend, get them as a list of dicts in Python!Example.
import plotly.express as px import marimo as mo from vega_datasets import data _plot = px.scatter( data.cars(), x="Horsepower", y="Miles_per_Gallon", color="Origin" ) plot = mo.ui.plotly(_plot)
# View the plot and selected data mo.hstack([plot, plot.value])
Attributes.
value: a dict of the plot dataranges: the selection of the plot; this may be an interval along the name of an axis
Initialization Args.
figure: Aplotly.graph_objects.Figurelabel: optional text label for the elementon_change: optional callback to run when this element’s value changes
matplotlib#
- marimo.mpl.interactive(figure: Figure | Axes) Html#
Render a matplotlib figure using an interactive viewer.
The interactive viewer allows you to pan, zoom, and see plot coordinates on mouse hover.
Example:
plt.plot([1, 2]) # plt.gcf() gets the current figure mo.mpl.interactive(plt.gcf())
Args:
figure: a matplotlib
FigureorAxesobject
Returns:
An interactive matplotlib figure as an
Htmlobject