marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
5.31k stars 155 forks source link

Plotting in a loop #1662

Closed marcus1487 closed 1 week ago

marcus1487 commented 1 week ago

Documentation is

Explain in Detail

I want to create a set of plots for an unknown number of items. The obvious way to do this would be in a loop, but the documentation only provides the option for showing a plot by making the plot the last line of a notebook. This is not feasible (that I am aware) for an unknown number of plots. Is there a good workaround for this? It would be nice to have this in the docs if there is a good workaround as this is quite a common access pattern for me.

Your Suggestion for Changes

Add docs for workaround to plot variable number of plots from a single cell.

mscolnick commented 1 week ago

what library are you using for plotting? some come with built in ways to combine multiple, while for others, you can use marimo's layouting elements

https://docs.marimo.io/api/layouts/index.html

for Altair, you have

You could also use marimo layouting

charts = []
for d in data:
  charts.append(create_chart(d))

mo.vstack(charts)

We also support tabs and accordions to put them in.

Unfortunately, i dont think this specific case belongs in the docs. This is a good discussion for our discord though - feel free to jump in (https://marimo.io/discord)

marcus1487 commented 1 week ago

I am using plotnine, but exporting plots to matplotlib for the purposes of display in marimo. I will try the layout options. Layout options seem well suited to this task. I will give these a go and let you know if I have any issues. Thank you so much for the prompt response.

mscolnick commented 1 week ago

No problem!

marcus1487 commented 1 week ago

Worked like a charm. Thank you again!