Matplotlib imshow with EPS Matplotlib imshow with EPS python python

Matplotlib imshow with EPS


This is an old question, but worth answering for current and future visitors wanting to do the same.

There is a way to load SVG files and convert them into Path objects, implemented in the svgpath2mpl package. From the project's page:

A path in SVG is defined by a 'path' element which contains a d="(path data)" attribute that contains moveto, line, curve (both cubic and quadratic Béziers), arc and closepath instructions. Matplotlib actually supports all of these instructions natively but doesn't provide a parser or fully compatible API.

Seen at Matplotlib custom marker/symbol.


I usually achive a better rendering in this case with something like:

from PIL import Imagelogo = Image.open('icons\logo.png')# TODO define bbox as a matplotlib.transforms.Bboximage_axis.imshow(logo, clip_box=bbox, aspect='equal',   origin='lower', interpolation='nearest')

(Matplotlib 1.4.2, if you use an old version playing with interpolation and origin options may help)


I removed my code snippet as it was not useful.

It is not possible to use native SVG as image in Matplotlib. Matplotlib supports natively only png and then falls back to Pillow which does not support svg either, see http://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html or matplotlib image documentation.

Pillow supports eps, so that will work!(I am not sure what matplotlib will make of it internally)

I use it like this :

# Pillow must be thereimport matplotlib.image as mpimgdef Save_svg(fig):    # expects a matplotlib figure    # And puts a logo into a frameless new axes    imagefile = 'logo.eps'    svgfile   = 'output.svg'    logoax    = [0.265, 0.125, 0.3, 0.3]    img=mpimg.imread(imagefile)    #    newax = fig.add_axes(logoax, anchor='SW', zorder=100)    newax.imshow(img)    newax.axis('off')    #    fig.savefig(svgfile,dpi=300)