【问题标题】:How do I export images of graphs to a pdf in Jupyter?如何在 Jupyter 中将图形图像导出为 pdf?
【发布时间】:2020-04-10 00:02:47
【问题描述】:

我不希望将整个笔记本导出为 pdf - 我已经搜索并找到了该问题的解决方案。我只想将笔记本中的绘图导出为 pdf。是否有 Python 库允许这样做?

【问题讨论】:

  • 你在使用matplotlib吗?您可以将这些数字另存为 pdf directly
  • 谢谢。我遇到了这个:stackoverflow.com/questions/13642528/…
  • 我想要做的是将多个图表导出到一个 pdf 中。我想另一种选择是合并 pdf?
  • 是的,我明白了。您可以尝试使用subplots 将所有图表组合在一个图中,但我知道这可能会变得笨拙。另一种选择是将它们全部保存为 .pngs,然后使用pdflatex 生成一个包含所有您喜欢的数字的漂亮报告。不知道纯python/matplotlib有没有好办法。

标签: python jupyter-notebook jupyter


【解决方案1】:

Jupyter nbconvert 命令允许指定自定义模板。

Michael Goerz 在这里为 LaTeX/PDF 编写了一个完整的自定义模板: https://gist.github.com/goerz/d5019bedacf5956bcf03ca8683dc5217

要只打印图表,您可以修改它以空白除输出单元格以外的任何部分,如下所示:

% 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

要生成 LaTeX 文件,请将上面的内容另存为 custom_article.tplx 并运行:

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

在单个命令中生成 LaTeX 文件和 PDF:

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

【讨论】:

    【解决方案2】:

    这可能不是最优雅的答案,但如果您想要做的不仅仅是将每个图放在页面上,它也非常灵活。您可以使用LaTeX 将所有图形导出为图像后收集到一个 pdf 中。这是一个示例,我们将图形保存为report/imgs/*.png,然后编写一个report/report.tex 文件并将其与pdflatex 编译成最终的report/report.pdf

    import numpy as np
    import matplotlib.pyplot as plt
    

    创建并保存两个图像:

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

    编写一个.tex 文件以显示两个图像:

    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}
    

    最后编译:

    !cd report && pdflatex report.tex
    

    【讨论】:

    • 从您的方法中汲取灵感,一个更优雅的替代方案可能是将其转换为 jupyter nbconvert 的自定义 LaTeX 模板。
    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 2013-11-20
    相关资源
    最近更新 更多