Jupyter notebook matplotlib figures missing in exported pdf
Solution 1
If you change the %matplotlib notebook
to %matplotlib inline
, then PDF export with jupyter's nbconvert works fine for me. The %matplotlib notebook
magic renders the plot in an interactive way that I suspect isn't properly recognized by LaTeX, which is used during the PDF conversion.
Alternatively, if you have to use %matplotlib notebook
for some reason, the export to HTML with jupyter's nbconvert (jupyter nbconvert --to='html' Test.ipynb
) seems to preserve the plot. I am then able to print this HTML file to a PDF from a web browser and the plot is present in the final PDF.
Solution 2
Crude solution to interactive problem: in interactive function save figure to a file
fig.savefig("pieCharts.png")
in next cell display a file:
from IPython.display import Image
Image("pieCharts.png")
In PDF only output of the 2nd line will be displayed with image as changed in interactive function.
areth
Updated on June 06, 2022Comments
-
areth almost 2 years
When generating a pdf in jupyter notebook, everything works great, but I would like to keep the inline figures inside the pdf, as well as the notebook.
Here is my code:
%matplotlib notebook import matplotlib.pyplot as plt import numpy as np from IPython.display import set_matplotlib_formats set_matplotlib_formats('png', 'pdf') save_figures = True x = np.arange(0, 20, 0.1) y = np.sin(x) plt.figure() plt.plot(x, y) if save_figures: plt.savefig('test.png') plt.show()
The figures appear inline, but in the pdf what is printed instead is:
<IPython.core.display.Javascript object> <IPython.core.display.HTML object>
The same thing appears in the pdf if I export from the web or use
nbconvert
to export to pdf from the command line.Are there any additional commands that I need to invoke in order to get this to work?
-
areth about 8 yearsPerfect! Yep, interactive plots don't seem to be rendered but using
inline
works fine. Thank you! -
Tom almost 7 yearsDo you know if there's a way to override
%matplotlib notebook
from the command line so static figures are being exported instead? -
wander95 over 5 years
%matplotlib inline
did it for me. -
phhu almost 3 yearsThis worked for me for PDF export, and also solved issues with the notebook file size and image caching.