You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Inkscape正确识别Matplotlib生成的Times New Roman字体转EPS?

问题描述

我常用Matplotlib绘制图表,以下是最小可运行示例:

def cm2inch(*tupl):
    inch = 2.54
    if isinstance(tupl[0], tuple):
        return tuple(i/inch for i in tupl[0])
    else:
        return tuple(i/inch for i in tupl)

# matplotlib settings
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from matplotlib.patches import Ellipse, Rectangle
import matplotlib.patches as patches
import numpy as np

plt.rc('text', usetex=True)
plt.rc('font',**{'family':'serif','serif':['Times New Roman']})
params = {'text.latex.preamble' : [r'\usepackage{amssymb}', r'\usepackage{amsmath}']}
plt.rcParams.update(params)

figW = 17.9219/2
figH = 3.5*2.0
fig = plt.figure(figsize=cm2inch((figW, figH)))

x = np.linspace(0.001, 15., 500)

ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8],
                    xlabel=r"x-axis",ylabel=r"y-axis")
xr_ = 0.5
ax1.axvspan(5-xr_, 5+xr_, color='C1', alpha=0.2, linewidth=0)
ax1.plot(x, 0.05*(x-5)**2, 'C0', label='cool plot')
ax1.axvline(5, color='C3', dashes=[4,2], lw=1.0)

ax1.legend(fontsize=6, loc=1)
ax1.set_xlim([0, 14.5])
ax1.set_ylim([0,1.1])

ax1.set_xticks([])
ax1.set_xticklabels([])
ax1.set_yticks([0, 1.0])
ax1.set_yticklabels([0, 1])
ax1.tick_params(axis="y",direction="in")
ax1.tick_params(axis="x",direction="in")

yshift_ = 0.1
yshift_2 = 0.3
ax1.text(2*5-2.5, 0.48+yshift_2, r"value", fontsize=8, color='C3', ha='center')

fig.savefig('input.pdf', dpi=1000)
plt.show()

图表输出示例:
图表输出示例

我需要将生成的PDF转换为EPS(因工作流原因,不想直接生成EPS)。试过用Ghostscript转换,但由于PDF包含透明元素,转换时会被光栅化,不符合需求。

改用Inkscape处理,执行命令:

inkscape input.pdf --export-filename=output.eps

Inkscape可以保留矢量元素不光栅化,但无法正确识别Matplotlib生成的Times New Roman字体,转换后字体异常:
转换后字体异常的输出

是否有方法让Inkscape正确转换Matplotlib生成的Times New Roman字体?

补充信息:使用Mac OS,已通过字体册安装Times New Roman字体。


解决方法

1. 确保Matplotlib生成的PDF正确嵌入Times New Roman字体

修改Matplotlib的保存设置,强制嵌入字体,避免字体仅被引用而未嵌入:
在fig.savefig时添加关键参数:

fig.savefig('input.pdf', dpi=1000, bbox_inches='tight', pad_inches=0.05,
            embed_fonts=True)  # 强制嵌入字体,避免字体缺失

若启用了usetex=True,可修改LaTeX预加载配置,确保输出PDF的字体嵌入更规范:

  • 若LaTeX环境支持XeLaTeX/LuaLaTeX:
params = {'text.latex.preamble' : [
    r'\usepackage{amssymb}', 
    r'\usepackage{amsmath}',
    r'\usepackage{fontspec}',
    r'\setmainfont{Times New Roman}'
]}
plt.rcParams.update(params)
  • 若使用pdfLaTeX,可改用LaTeX原生的Times替代包:
params = {'text.latex.preamble' : [
    r'\usepackage{amssymb}', 
    r'\usepackage{amsmath}',
    r'\usepackage{newtxtext}',
    r'\usepackage{newtxmath}'
]}
plt.rcParams.update(params)

2. 调整Inkscape的字体匹配参数(Mac OS)

Mac上的Times New Roman字体名称可能与Inkscape的识别逻辑存在差异,可通过命令行参数强制匹配:

inkscape input.pdf --export-filename=output.eps --pdf-poppler --replace-fonts="Times New Roman=Times New Roman"

参数说明:

  • --pdf-poppler:使用Poppler引擎解析PDF,提升字体识别准确率
  • --replace-fonts:强制指定字体映射规则,确保Inkscape匹配系统中的Times New Roman

也可通过图形界面配置:打开Inkscape偏好设置→字体→启用“替换缺少的字体”,并将Times New Roman设为首选替换字体。

3. 文本转曲(可选,彻底规避字体问题)

如果不需要保留文本的可编辑性,可在转换时将文本转为矢量路径,彻底避免字体识别问题:

inkscape input.pdf --export-filename=output.eps --text-to-path=true

内容的提问来源于stack exchange,提问作者Wolpertinger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 01:54:13