将 matplotlib 图形输出到 SVG,其中文本为文本,而不是曲线

问题描述 投票:0回答:1

当我使用

matplotlib.pyplot.savefig("test.svg", format="svg")
将图形导出为 SVG 时,生成的 SVG 文件很大。

这是由于我的图中有很多文本注释,并且每个文本最终都作为 SVG 中的路径造成的。

我希望我的文本最终成为 SVG 中的文本字符串,而不是路径。如果以这种方式导出文本字符串,则解释输出也变得太困难。

有没有办法强制 matplotlib 将文本输出为文本,而不是曲线?

目前,我在 SVG 文件中看到这些代码片段:

<path d=" M9.8125 72.9062 L55.9062 72.9062 L55.9062 64.5938 L19.6719
64.5938 L19.6719 43.0156 L54.3906 43.0156 L54.3906 34.7188 L19.6719
34.7188 L19.6719 8.29688 L56.7812 8.29688 L56.7812 0 L9.8125 0 z "
id="DejaVuSans-45" />
svg matplotlib
1个回答
82
投票

Matplotlibs SVG 文本渲染可以在 matplotlibrc 或代码中配置。
来自使用样式表和 rcParams 自定义 Matplotlib

#svg.fonttype : 'path'         # How to handle SVG fonts:
#    'none': Assume fonts are installed on the machine where the SVG will be viewed.
#    'path': Embed characters as paths -- supported by most SVG renderers
#    'svgfont': Embed characters as SVG fonts -- supported only by Chrome,
#               Opera and Safari

这将转换为以下代码,既不嵌入字体也不将文本呈现为路径:

import matplotlib.pyplot as plt
plt.rcParams['svg.fonttype'] = 'none'
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.