You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Inkscape中导入带TeX字体的Matplotlib LaTeX文本?

Making Matplotlib's LaTeX Text Editable in Inkscape

Let's break down your problem first: you've set svg.fonttype='none' (which is correct for preserving text objects instead of paths), but enabling usetex=True overrides this behavior. When you use external TeX rendering, Matplotlib calls your system's TeX engine to render all text elements, then converts those rendered outputs into vector paths for the SVG. That's why Inkscape sees them as uneditable paths instead of text.

Here are two practical solutions depending on your needs:

Matplotlib has its own LaTeX-compatible rendering engine called mathtext, which lets you use LaTeX syntax while keeping text as editable objects in SVG exports. Adjust your code like this:

from scipy.stats import lognorm
from matplotlib import rc
import matplotlib.pyplot as plt
import numpy as np
import os
import matplotlib

# Keep this to preserve text objects in SVG
matplotlib.rcParams['svg.fonttype'] = 'none'
# Disable external TeX, use Matplotlib's built-in mathtext
rc('text', usetex=False)
rc('font', family='Times New Roman')
rc('font', size='20.0')
# Optional: Match Times New Roman style for math symbols
rc('mathtext', fontset='stix')  # Stix fonts align well with Times New Roman

mu = 1.7
sigma = 1.1
data = np.arange(0,100)
# Note: `normed=True` is deprecated, use `density=True` instead
n, bins, patches = plt.hist(data, bins=10000, facecolor='k', edgecolor='k', density=True, alpha=0.3, histtype='stepfilled', label='Empirical data')
y = lognorm.pdf(bins, sigma, scale=np.exp(mu))
plt.xlim(0,50)
plt.plot(bins, y, '-', color='k', linewidth=2, label='Lognormal curve')
plt.ylim(0, .15)
plt.xlabel('my x label')
plt.ylabel('my y label')
plt.grid()
plt.legend()
plt.savefig(os.path.expanduser('~/myfile.svg'))

When you export the SVG with this code, all text (including any LaTeX-formatted content you might add later) will be fully editable in Inkscape.

Solution 2: Stick to External TeX? Export PDF Instead

If you absolutely need to use your system's TeX engine (e.g., for custom packages or rare fonts), skip SVG and export a PDF instead:

plt.savefig(os.path.expanduser('~/myfile.pdf'))

Then open this PDF directly in Inkscape. Most TeX-rendered text in PDFs will be recognized as editable text objects by Inkscape, which avoids the path conversion issue entirely.

The "Path to Text" feature in Inkscape is a last resort here, but it's unreliable—it tries to guess the text from path shapes, which often leads to errors or misrecognized characters.

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

火山引擎 最新活动