IamWarmduscher
IamWarmduscher

Reputation: 955

How do I export images of graphs to a pdf in Jupyter?

I do not wish to export the entire notebook as a pdf - I've searched and found solutions for that problem. I want to only export the plots in my notebook to a pdf. Is there a Python library does allows for this?

Upvotes: 2

Views: 7328

Answers (2)

anjsimmo
anjsimmo

Reputation: 714

The Jupyter nbconvert command allows specifying a custom template.

Michael Goerz has written a full custom template for LaTeX/PDFs here: https://gist.github.com/goerz/d5019bedacf5956bcf03ca8683dc5217

To only print the graphs, you could modify it to blank out any sections other than the output cells, like so:

% Tell the templating engine what output template we want to use.
((* extends 'article.tplx' *))

% Template will setup imports, etc. as normal unless we override these sections.

% Leave title blank
((* block title -*))
((*- endblock title *))

% Leave author blank
((* block author -*))
((* endblock author *))

% Etc.
((* block maketitle *))
((* endblock maketitle *))

% Don't show "input" prompt
((*- block in_prompt -*))
((*- endblock in_prompt -*))

% Hide input cells
((*- block input -*))
((*- endblock input -*))

% Don't show "output" prompt
((*- block output_prompt -*))
((*- endblock output_prompt -*))

% Let template render output cells as usual

To generate a LaTeX file, save the above as custom_article.tplx and run:

jupyter nbconvert --to=latex --template=custom_article.tplx file.ipynb

To generate the LaTeX file and PDF in a single command:

jupyter nbconvert --to=pdf --template=custom_article.tplx file.ipynb

Upvotes: 2

l_l_l_l_l_l_l_l
l_l_l_l_l_l_l_l

Reputation: 538

This is probably not the most elegant answer, but it's also extremely flexible in case you want to do more than just put each plot on a page. You can use LaTeX to collect all graphs in a single pdf after exporting them as images. Here's an example where we save the graphs as report/imgs/*.png, then write a report/report.tex file and compile it with pdflatex into a final report/report.pdf.

import numpy as np
import matplotlib.pyplot as plt

Create and save two images:

plt.bar(np.arange(5), np.arange(5)*2)
plt.savefig('report/imgs/fig1.png')
plt.bar(np.arange(6), np.arange(6)**2 - 1, color = 'g')
plt.savefig('report/imgs/fig2.png')

Write a .tex file to display both images:

img_data = ['fig1', 'fig2']

latex_src  = '\\documentclass{article}\n'
latex_src += '\\usepackage{graphicx}\n'
latex_src += '\\graphicspath{{./imgs/}}\n'
latex_src += '\\begin{document}\n'

for image in img_data:
    latex_src += '\t\\begin{figure}[h]\n'
    latex_src += f'\t\t\\includegraphics{{{image}}}\n'
    latex_src += '\t\\end{figure}\n'

latex_src += '\\end{document}'

with open('report/report.tex', 'w', encoding = 'utf-8') as handle:
    handle.write(latex_src)

print(latex_src)
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{./imgs/}}
\begin{document}
    \begin{figure}[h]
        \includegraphics{fig1}
    \end{figure}
    \begin{figure}[h]
        \includegraphics{fig2}
    \end{figure}
\end{document}

And finally compile:

!cd report && pdflatex report.tex

Upvotes: 1

Related Questions