Get help from the marimo community

Updated 5 days ago

matplotlib plot only shown in edit mode

At a glance

The community member is using Marimo and is having an issue with displaying matplotlib plots in view mode, slide mode, or when running the notebook. The plots are only visible in edit mode. The community member has also tried using plotly and encountered the same issue. Altair plots, however, are visible in view mode. The community member has seen examples where matplotlib is used successfully in Marimo and has tried generating a WASM application, but the issue persists.

In the comments, another community member suggests that the issue might be related to using plt.show() or not using anything at all. They recommend using plt.gca() at the end of the cell blocks to display the plots in run mode. For plotly, they suggest using mo.ui.plotly.

The original community member confirms that the suggestion to use plt.gca() instead of plt.show() fixed the issue, and the plots are now displayed in view mode as well.

<answer>The issue was related to the community member using <code>plt.show()</code> instead of <code>plt.gca()</code> to display the matplotlib plots. Changing to <code>plt.gca()</</code>
Useful resources
Hello,

I have been trying Marimo over the weekend and I find it great.
All the problems bothering me in Jupyter Notes and some I didn't even realize I had seem to be addressed in Marimo.

There is one thing I cannot seem to figure out:

If I create a plot (the most basic plot) using matplotlib it shows correctly as long as I'm in edit mode but as soon as
I switch to View mode, or slide mode, or I run marimo run my_notebook.py the matlib plots are not shown anymore.

I have also tried with plotly and I get the same problem, the plots are only visible when the notebook is in edit mode.

altair plots seem to be fine though ( they are also visible when I switch to view mode).

I thought this might be a limitation but then I saw examples where matplotlib is being used and the plots are successfully displayed,
like the example: Neural Networks with Micrograd
https://marimo.io/p/@marimo/micrograd

I've seen that the exmaple uses WASM and tried to generate a WASM application from my notebook using:
Plain Text
marimo export html-wasm notebook.py -o notebook.wasm.html

and then serving the generated html.

This did not seem to make any difference, the matplotlib plots still do not show.

Am I missing something ?

Thank you in advance for any answers or suggestions.
Marked as solution
Mostly, the issue would be relating to you using plt.show() or maybe even nothing.
View full solution
H
R
7 comments
@RaduW Can you provide sample codes of the cell blocks where you're using matplotlib to plot?
Mostly, the issue would be relating to you using plt.show() or maybe even nothing.
Use plt.gca() at the end of the cell blocks and you should see the plots being displayed even in run mode.
For plotly, are you using mo.ui.plotly? See relevant docs here and here.
@Haleshot thank you very much, your suggestion fixed it! I was indeed using plt.show().
After changing to plt.gca() it started to display the plot also in view mode ( now it displays the plot above the
cell and it used to display below ).
That's great! I wonder if the docs for matplotlib in marimo docs reflect this as this issue from creeping up every now and then.
@Haleshot the docs do reflect the proper way to use matplotlib https://docs.marimo.io/guides/working_with_data/plotting/#matplotlib , somehow I totally missed it.

I am used to using plt.show() and seeing it working in edit mode made me think that the problem might be related to properly configuring the drawing backend, or compiling the notebook in WASM ... anything but returning the axes from the cell (which I still find strange, but that's irrelevant).

Perhaps putting the information in a shoutout would help stand out.

![Note]
To output a matplotlib plot in a cell's output area, include its Axes or Figure object as the last expression in your notebook.
Add a reply
Sign up and join the conversation on Discord