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.
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
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.
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.
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.
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).