Search code examples
pythonsvgtextlatexpgf

How do I save the text in python plots editable in svg files?


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')

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.

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')

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

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


Solution

  • Found an answer. You can use

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

    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?.