Saving SVG vector graphics from Matplotlib, usable in LaTeX

Related: include SVG in LaTeX

from matplotlib.pyplot import figure, draw

data = [1,2,3,4]

fg = figure()
ax = fg.gca()
ax.plot(data)

draw()
fg.savefig('myfig.svg', bbox_inches='tight')

The advantages of saving SVG from Matplotlib instead of EPS or PNG include:

  • vector graphics (SVG or EPS) allow nearly infinite zooming without loss of quality–excellent for line plots and contour plots
  • SVG is viewable by just about anything including web browsers.