如何在svg文件中可编辑的python图中保存文本? [英] How do I save the text in python plots editable in svg files?

查看:68
本文介绍了如何在svg文件中可编辑的python图中保存文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将python图另存为svg文件:

I am trying to save a python plot as an svg file:

import matplotlib as mpl
mpl.use('svg')
import matplotlib.pyplot as plt
import numpy as np

plt.figure()
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('Phase $\phi$')
plt.ylabel('Signal')
plt.savefig('SineSignal.svg', format = 'svg')

到目前为止有效.但是,一旦我在Inkscape中打开文件,就无法再编辑文本了.好像python将文本保存为图形而不是文本.因此,我既无法在Inkscape中更改字体,字体大小等,也无法在我使用乳胶创建的PDF文件中的图中搜索文本元素.

Works so far. But once I open the file in Inkscape, I cannot edit the text anymore. Seems like python saved the text as a graphic instead as text. Because of this I am neither able to change font, fontsize etc. in Inkscape nor to search for text elements in the plots in the PDF file I create with latex.

另一种选择是将图保存为PGF(在这种情况下,必须将mpl.use('svg')替换为mpl.use('pgf')):

Another option is to save the plot as PGF (mpl.use('svg') has to be replaced with mpl.use('pgf') in this case):

plt.savefig('SineSignal.pgf')

这样,我仍然无法编辑字体/字体大小,但是至少我可以在pdf中搜索文本元素.

This way I am still not able to edit font/fontsize, but at least I can search for textelements in the pdf.

有什么建议吗?不能在python中使用TikZ,因为其功能非常有限,并且绘图看起来会有所不同.

Any suggestions? Using TikZ in python is not an option because the features are quite limited and the plots would look different.

推荐答案

找到了答案.您可以使用

Found an answer. You can use

new_rc_params = {'text.usetex': False,
"svg.fonttype": 'none'
}
mpl.rcParams.update(new_rc_params)

防止svg后端将文本呈现为路径.有关更多详细说明,请参见

to prevent the svg backend from rendering the text as paths. For more detailed instructions take a look at Is there an efficient way to store 2D plots as a vector graphic in python?.

这篇关于如何在svg文件中可编辑的python图中保存文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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