【问题标题】:Save matplotlib figures as .pdf_tex将 matplotlib 图形另存为 .pdf_tex
【发布时间】:2018-10-09 09:47:47
【问题描述】:

在 Inkscape 中,可以保存 pdf 文件以及生成 pdf_tex 文件以自动格式化为 Latex 文件。 这可以在单击将扩展名指定为 .pdf 保存后选中一个复选框。

我有兴趣直接从我的 python 脚本中以特定格式保存 matplotlib 图形。

这种可能性是否存在或者可以通过一些技巧来完成?

谢谢

【问题讨论】:

  • Those are the options 我在inkscape中看到。你指的是哪一个?
  • 对不起,我不够具体。您可以打开一个文件,将其另存为 pdf 时,可以选择生成 .pdf_tex 文件和 .pdf 文件

标签: python matplotlib latex inkscape


【解决方案1】:

一种选择是从 matplotlib 导出 pdf,然后以编程方式使用 inkscape,即通过命令行界面让它创建所需的格式。

import matplotlib.pyplot as plt

plt.bar(x=[1,2], height=[3,4], label="Bar")
plt.legend()
plt.xlabel("Label")
plt.title("title")

def savepdf_tex(fig, name, **kwargs):
    import subprocess, os
    fig.savefig("temp.pdf", format="pdf", **kwargs)
    incmd = ["inkscape", "temp.pdf", "--export-pdf={}.pdf".format(name),
             "--export-latex"] #"--export-ignore-filters",
    subprocess.check_output(incmd)
    os.remove("temp.pdf")


savepdf_tex(plt.gcf(), "latest")

plt.show()

还要注意 matplotlib 可以保存为pgf 格式。作为上述方法的替代品,绝对值得尝试。请参阅this example,并将plt.savefig("filename.pgf") 附加到它上面。在乳胶代码中使用\input{filename.pgf}

【讨论】:

  • 返回错误FileNotFoundError: [WinError 2] The system cannot find the file specified
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 2015-12-02
  • 2021-10-14
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
相关资源
最近更新 更多