将matplotlib图形输出到SVG,文本作为文本,而不是曲线 [英] Output matplotlib figure to SVG with text as text, not curves

查看:227
本文介绍了将matplotlib图形输出到SVG,文本作为文本,而不是曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I use matplotlib.pyplot.savefig("test.svg", format="svg") to export the figure as SVG, then the resulting SVG file is huge.

这是由于以下事实造成的:我的图中有很多文本注释,并且每个文本最终都成为SVG中的路径.

This is caused by the fact that there are a lot of text annotations in my figure, and each text ends up as paths in the SVG.

我希望我的文本以SVG中的文本字符串结尾,而不是路径.如果以这种方式导出文本字符串,那么也很难解释输出.

I want my text to end up as text strings in SVG, and not paths. It gets too hard to interpret the output too, if the text strings are exported this way.

是否有一种方法可以强制matplotlib将文本输出为文本,而不是曲线?

Is there a way to force matplotlib to output text as text, not curves?

当前,我在SVG文件中看到以下代码片段:

Currently, I see these code fragments in my SVG file:

<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" />

推荐答案

Matplotlibs可以在matplotlibrc或代码中配置SVG文本呈现.来自 http://matplotlib.org/users/customizing.html :

Matplotlibs SVG text rendering can be configured either in the matplotlibrc or in code. From http://matplotlib.org/users/customizing.html:

#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

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

This translates to the following code for neither embedding the font nor rendering the text as path:

import matplotlib.pyplot as plt
plt.rcParams['svg.fonttype'] = 'none'

这篇关于将matplotlib图形输出到SVG,文本作为文本,而不是曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆