使用 LaTeX 字体将 Matplotlib 图形保存为 eps

问题描述

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

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

建议?解决方法

谢谢, 拉多万

附注。我发现的一种解决方法是使用 gnuplot 和 cairolatex 终端......生成的 *.tex 文件可以用 Pdflatex 编译,结果非常好。但那是另一个故事:-)。

解决方法

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

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",transparent=True)

plt.show()

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

干杯, 拉多万