Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output matplotlib figure to SVG with text as text, not curves

Tags:

matplotlib

svg

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

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.

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.

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

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" /> 
like image 325
Bram Avatar asked Dec 21 '15 01:12

Bram


1 Answers

Matplotlibs SVG text rendering can be configured either in the matplotlibrc or in code.
From Customizing Matplotlib with style sheets and rcParams:

#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' 
like image 102
zliw Avatar answered Sep 20 '22 12:09

zliw