Back to Marimo

Plotting

docs/api/plotting.md

0.23.54.1 KB
Original Source

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 plots with matplotlib

::: marimo.ui.matplotlib

Reactive charts with Altair

/// marimo-embed size: large

python
@app.cell
async def __():
    import altair as alt
    import pandas as pd
    import json
    return

@app.cell
def __():
    cars = pd.read_json(
      'https://vega.github.io/vega-datasets/data/cars.json'
    )

    chart = mo.ui.altair_chart(alt.Chart(cars).mark_point().encode(
        x='Horsepower',
        y='Miles_per_Gallon',
        color='Origin'
    ))
    return

@app.cell
def __():
    mo.vstack([chart, mo.ui.table(chart.value)])
    return

///

Disabling automatic selection

marimo automatically adds a default selection based on the mark type, however, you may want to customize the selection behavior of your Altair chart. You can do this by setting chart_selection and legend_selection to False, and using .add_params directly on your Altair chart.

python
# Create an interval selection
brush = alt.selection_interval(encodings=["x"])

_chart = (
    alt.Chart(traces, height=150)
    .mark_line()
    .encode(x="index:Q", y="value:Q", color="traces:N")
    .add_params(brush) # add the selection to the chart
)

chart = mo.ui.altair_chart(
    _chart,
    # disable automatic selection
    chart_selection=False,
    legend_selection=False
)
chart # You can now access chart.value to get the selected data

::: marimo.ui.altair_chart

Performance and Data Transformers

Altair has a concept of data transformers, which can be used to improve performance.

Some examples are:

  • pandas Dataframe has to be sanitized and serialized to JSON;
  • the rows of a Dataframe might need to be sampled or limited to a maximum number;
  • the Dataframe might be written to a .csv or .json file for performance reasons.

By default, Altair uses the default data transformer, which is the slowest in marimo. It is limited to 5000 rows (although we increase this to 20_000 rows as marimo can handle this). This includes the data inside the HTML that is being sent over the network, which can also be limited by marimo's maximum message size.

It is recommended to use the marimo_csv data transformer, which is the most performant and can handle the largest datasets: it converts the data to a CSV file which is smaller and can be sent over the network. This can handle up to +400,000 rows with no issues.

When using mo.ui.altair_chart, we automatically set the data transformer to marimo_csv for you. If you are using Altair directly, you can set the data transformer using the following code:

python
import altair as alt
alt.data_transformers.enable('marimo_csv')

Reactive plots with Plotly

!!! warning "Supported charts"

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

::: marimo.ui.plotly

Interactive matplotlib

::: marimo.mpl.interactive options: show_root_heading: true show_source: true

Leafmap

marimo supports rendering Leafmap maps using the folium and plotly backends.

Other plotting libraries

You can use all the popular plotting libraries with marimo, such as: