Plots generated with %matplotlib widget are not being saved when converting to HTML file

Hello,

I am trying to create notebook reports for archiving purposes, but it seems that the widget output is not saved.

Steps to reproduce:

102 with nxcals pro, in a notebook called Untitled:

import matplotlib.pyplot as plt
import os
import getpass
import sys

%matplotlib widget
plt.plot([0, 1, 2, 3, 4], [0, 3, 1, 2, 5])
plt.show()

os.system(f'{sys.executable} -m jupyter nbconvert --to html Untitled.ipynb')
os.system(f'mv Untitled.html /eos/user/s/{getpass.getuser()}/')

Thanks in advance for your precious help.

Dear Simon,

You can save the plot by using the following code:

import matplotlib.pyplot as plt
import os
import getpass
import sys

# Create the plot
plt.plot([0, 1, 2, 3, 4], [0, 3, 1, 2, 5])

# Save the plot as an image
plt.savefig('/eos/user/s/YOURUSER/plot.png')

# Show the plot
plt.show()

# Convert notebook to HTML
os.system(f'{sys.executable} -m jupyter nbconvert --to html Untitled.ipynb')
os.system(f'mv Untitled.html /eos/user/s/{getpass.getuser()}/')

Let me know if this works for you. I tested it and I could see the plot in the html file.

Best,
Pedro

Dear Petro,

Thank you for this answer. However, I might not have been clear explaining my problem. I am developing notebooks for the MP3 group, and they would like to have both interactive notebooks (created with the magic command %matplotlib widget) and html reports of the notebook (created with the jupyter nbconvert command). The problem I’m facing is that interactive plots are not being saved in the html report, even though this behaviour works in vscode or in a “classic” jupyter environment.

My quick fix was to add this comment to every plot:

%matplotlib inline
# To display plots dynamically run the following line (don't forget to put again inline and run the cell again for the plot to appear in the report)
# %matplotlib widget

However, this solution is far for being ideal given that matplotlib widget is not the default and that one has to rerun the cell to be able to zoom, and that one need to remember to rerun the cell with matplotlib inline for the plot to appear in the report.

I hope those explanations were clearer and that you will be able to solve this issue :slight_smile:

Dear Simon,

So if I understand correctly, you want the generated HTML to have interactive plots? Or just a static image would do?

Cheers,
Enric

Dear Enric,

I would like to have the images being static in the generated report indeed. Having interactive plots in the generated HTML report would be a nice to have, but it’s out of the scope of the issue.

Cheers,
Simon

Dear Simon,

It might be an issue with the nbconvert version that is in SWAN, I see this issue:

Are you running this from the SWAN Classic UI or from the new JupyterLab (Alma9)? I believe it’s worth trying in JupyterLab since there we have newer versions of all Jupyter packages.

Cheers,
Enric

Given that our notebooks requires NXCALS accees, we are stuck using 102b and CC7.

Can you check if it works with %matplotlib notebook + jupyter nbconvert as suggested in the issue linked above?

%matplotlib notebook used to be working, but we had to pin it to version matplotlib==3.1.3 due to another bug (see Bug in interactive matplotlib plots in SWAN notebooks)

The problem is that we wanted to remove that old dependancy, so we switched to the notebook backend, created new plots in our notebook and now it would be a great effort to migrate everything back.

Also, fyi, the ipympl package (i.e. the one responsable for the %matplotlib widget magic command) is not installed on the JupyterLab environment.

Can you please check if the conversion to HTML itself works well (with matplotlib widget) if you select LCG 105a (not NXCALS), CentOS7?

If that is so, we are now in the process of creating a new NXCALS software stack that will be based on LCG 105a, so we would just wait until that is available and the problem would be solved.

It looks like I’m unable to generate %matplotlib widget plots with 105b somehow. However, if I update ipympl to latest and I restart the kernel, then it looks like everything is working as espected. Therefore, I think nxcals LCG 105a would be solving my issue.

Ok, good to hear. You can also request the SPI librarians (https://its.cern.ch/jira/projects/SPI/issues) to update ipympl in the LCG releases. That won’t make it into 105a, but it will be there from 106 on (and therefore in the next NXCALS stack, whenever it’s created).