Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter notebook matplotlib figures missing in exported pdf

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?

like image 492
areth Avatar asked Feb 10 '16 01:02

areth


1 Answers

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.

like image 60
Michelle Lynn Gill Avatar answered Oct 15 '22 02:10

Michelle Lynn Gill