RaduW
RaduW2w ago

matplotlib plot only shown in edit mode

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:
marimo export html-wasm notebook.py -o notebook.wasm.html
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.
marimo | Neural Networks with Micrograd
Explore data and build apps seamlessly with marimo, a next-generation Python notebook.
5 Replies
Hall
Hall2w ago
Someone will reply to you shortly. In the meantime, this might help: -# This post was marked as solved by Myles Scolnick. View answer.
Haleshot
Haleshot2w ago
@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.
RaduW
RaduWOP2w ago
@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 ).
Haleshot
Haleshot2w ago
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.
RaduW
RaduWOP2w ago
@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.

Did you find this page helpful?