Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib - plot outputs text as paths and cannot be converted to LaTeX by Inkscape

Tags:

I have a matplotlib plot that I would like to save in a vector graphics format to then use in a LaTeX document.

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

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.

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.

Thanks.

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'))
like image 877
gozzilli Avatar asked Jan 30 '13 09:01

gozzilli


2 Answers

I ran into the same problem and fixed it.

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'

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.

like image 85
Antonio El Khoury Avatar answered Sep 16 '22 21:09

Antonio El Khoury


Another, more permanent option, is to put

svg.fonttype : none

in your matplotlibrc (~/.config/matplotlib/matplotlibrc, for example)

like image 35
pstjohn Avatar answered Sep 19 '22 21:09

pstjohn