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'))
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.
Another, more permanent option, is to put
svg.fonttype : none
in your matplotlibrc (~/.config/matplotlib/matplotlibrc, for example)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With