首页 > 解决方案 > 将带有 LaTeX 字体的 Matplotlib 图保存为 eps

问题描述

我正在尝试使用 matplotlib 和 LaTeX 字体在 Jupyter-lab 中制作出版质量。我按照此处的说明创建了一个简单的文档: https ://matplotlib.org/stable/tutorials/text/usetex.html

图表是用漂亮的字体创建的;但是,保存的 eps 文件是空白的。相同的图形可以成功保存为 png,但即使使用dpi=2400选项导入到 LaTeX 时它看起来也很模糊。导入 LaTeX 文件时,没有 LaTeX 字体的 Eps 文件像剃刀一样锋利。

建议?解决方法?

谢谢,拉多万

PS。我发现的一种解决方法是使用 gnuplot 和cairolatex终端...生成的 *.tex 文件可以编译并Pdflatex获得出色的结果。但那是一个不同的故事:-)。

标签: matplotliblatexjupyter-labeps

解决方案


我又试了一次。最后,它确实奏效了!这是代码:

import numpy as np
import matplotlib.pyplot as plt

## reset defaults
plt.rcdefaults()

## Set up LaTeX fonts
plt.rcParams.update({
    "text.usetex": True,
    "font.family": "serif",
    "font.serif": ["Computer Modern Roman"],
    "font.size": 14,
    })

## Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots(figsize=(4.5,3))

ax.plot(t, s, linewidth=3)

ax.set(xlabel=r'time $\tau_p$ [$\mu$s]', ylabel=r'voltage (mV)')
ax.set(title=r'$ E = m c^2 $')

fig.savefig("test600.png", format="png", dpi=600, bbox_inches="tight")
fig.savefig("test1200t.eps", format="eps", dpi=1200, bbox_inches="tight", transparent=True)
fig.savefig("test1200t.pdf", format="pdf", dpi=1200, bbox_inches="tight", transparent=True)

plt.show()

我不确定之前出了什么问题。这次我做了不同的字体声明。

干杯,拉多万


推荐阅读