如何在 jupyter / matplotlib 图形下方添加乳胶图形标题

How to add latex figure captions below jupyter / matplotlib figures

在 jupyter notebook 中,有没有办法在每个内联 matplotlib 图下方添加乳胶图标题?这是需要的,以便在 运行 nbconvert --to latex.

时对每个图形进行注释

但我不清楚如何定位 LaTeX 相对于最终在 \begin{verbatim} 块中的图形。我可以把它放在情节之后的降价单元格中;但是,这并没有像我想要的那样包裹图形。

有点解决方法,但以下辅助函数调用 plt.close() 以防止显示内联图形,只留下为图形生成的 LaTeX 块。

bShowInline = True  # Set = False for document generation

def makeplot( plt, figlabel, figcaption):
    figname = figlabel+'.png'

    plt.savefig(figname)

    if bShowInline:
        plt.show()
    else:
        plt.close()

    strLatex="""
    \begin{figure}[b]
    \centering
        \includegraphics[totalheight=10.0cm]{%s}
        \caption{%s}
        \label{fig:%s}
    \end{figure}"""%(figname, figcaption, figlabel) 
    return display(Latex(strLatex)) 

有没有更简洁的方法?

您可以按照 Julius Schulz 先生的 Making Publication Ready Notebooks 博客 post 中的示例添加说明。该技术基本上归结为将标题添加为生成图形的单元格的 JSON 元数据的一部分,然后在传递给 nbconvert 的模板中提供正确的说明。我直接复制粘贴了 Julius 模板文件中绘制图形的部分,因为我对 Jinja 模板前端不太感兴趣。博客 post 对我很有帮助。