python - 将 matplotlib 图形另存为 .pdf_tex
问题描述
在 Inkscape 中,可以保存 pdf 文件以及生成 pdf_tex 文件以自动格式化为 Latex 文件。这可以在单击将扩展名指定为 .pdf 的保存后选中一个框来指定。
我有兴趣直接从我的 python 脚本中以特定格式保存 matplotlib 图形。
这种可能性是否存在或者可以通过一些技巧来完成?
谢谢
解决方案
一种选择是从 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
格式。作为上述方法的替代品,绝对值得尝试。请参阅此示例,并附plt.savefig("filename.pgf")
加到它。在乳胶代码中使用\input{filename.pgf}
.
推荐阅读
- java - 为视频播放器启动新活动时,如何强制android视频播放器在视频播放后不退出
- java - datagrip 如何从 couchbase 存储桶中获取所有文档?
- python - 无法使用硒单击阴影根元素
- java - 如何更新 TornadoFX 组合框 - Kotlin
- c# - 如何使单元格仅在填充后才能读取?数据网格视图 C#
- wordpress - Wordpress:add_submenu_page() 创建错误
- html - div 拆分窗格,最小宽度
- amazon-web-services - AWS Codebuild - 执行命令时出错:python -m pip install --upgrade --force pip。原因:退出状态1
- javascript - 当用户在提示中输入特定时间时,尝试让此代码显示问候语
- excel - 无法将 pandas/numpy 导入 xlwings 脚本