matplotlib - 绘图输出文本为路径,不能被 Inkscape 转换为 LaTeX [英] matplotlib - plot outputs text as paths and cannot be converted to LaTeX by Inkscape
问题描述
我有一个 matplotlib
图,我想将其保存为矢量图形格式,然后在 LaTeX 文档中使用.
I have a matplotlib
plot that I would like to save in a vector graphics format to then use in a LaTeX document.
我通常从 matplotlib
保存它,用 Inkscape 打开它并将其保存为 PDF+LaTeX(省略 PDF 中的文本并创建 LaTeX 文件).这也可以通过以下方式实现:
I normally save it from matplotlib
, open it with Inkscape and save it as PDF+LaTeX (omit text in PDF and create LaTeX file).
This can also be achieved with:
inkscape -D -z --file=in.pdf --export-pdf=out.pdf --export-latex
但是,对于下面的情节,文本实际上是一系列路径.每个字母都是独立的,导致 Inkscape 无法保存不同的 tex
文件.
However, for the following plot, the text is actually a series of paths. Each letter is separate, resulting in Inkscape not being able to save a different tex
file.
为什么在下面的代码中,文本没有呈现为文本,而是呈现为路径?请注意,使用 usetex=True
并没有什么不同.
Why is the text not rendered as text but as paths in the code below? Note that the use of usetex=True
does not make a difference.
谢谢.
from scipy.stats import lognorm
from matplotlib import rc
#rc('text', usetex=True)
rc('font', family='Times New Roman')
rc('font', size='20.0')
mu = 1.7
sigma = 1.1
n, bins, patches = plt.hist(data, bins=10000, facecolor='k', edgecolor='k',
normed=True, alpha=0.3, histtype='stepfilled',
label='\noindent Empirical data')
y = lognorm.pdf( bins, sigma, scale=np.exp(mu))
plt.xlim( (0,50) )
plt.plot(bins, y, '-', color='k', linewidth=2, label='\noindent 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'))
推荐答案
我遇到了同样的问题并解决了.
I ran into the same problem and fixed it.
http://matplotlib.org/users/customizing.html 中的 matplotlib 文档声明默认值为参数svg.fonttype
为'path'
,表示导出为svg格式时字符会转换为路径.
The matplotlib documentation in http://matplotlib.org/users/customizing.html states that the default value for the parameter svg.fonttype
is 'path'
, which means that characters will be converted to paths when exporting to svg format.
我需要做的就是在我的脚本中添加以下行:
All I needed to do was add the following line in my script:
matplotlib.rcParams['svg.fonttype'] = 'none'
这样所有字符都正确嵌入,我现在可以在 Inkscape 中编辑文本并将我的图形导出为 pdf+Latex,即一个 pdf 文件和一个包含在我的 tex
文件中的 pdf_tex 文件.
This way all characters are embedded correctly, I can now edit the text in Inkscape and export my figure to pdf+Latex, i.e. a pdf file and a pdf_tex file which I include in my tex
file.
这篇关于matplotlib - 绘图输出文本为路径,不能被 Inkscape 转换为 LaTeX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!