27

I want to generate a vector plot with matplotlib. I tried hard - but the output is a raster image. Here's what I use:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

and finally:

myfig.savefig('myfig.eps', format='eps')

I've found that export to ps gives a vector image, but the problem with eps remains.

3
  • 4
    Can you give an example (make up some data if you need to) of the actual calls to plt? This will help others contribute. Commented Feb 13, 2012 at 18:59
  • 1
    I am confident that the problem you are describing is not as you suggest and that the eps output really is a fully fledged vector rendition. It may be that you are looking at the eps through a poor viewer - which application are you using to view the eps? Commented Jul 17, 2012 at 10:15
  • 1
    plt.savefig('some_name.eps', bbox_inches='tight') produces a vectorized plot on my system. Commented Oct 21, 2015 at 15:17

4 Answers 4

23

I use the following code:

from matplotlib import pyplot as plt

fig, ax = plt.subplots() # or 
fig.savefig('filename.eps', format='eps')
Sign up to request clarification or add additional context in comments.

Comments

10

If you need emf files as output format, e.g. to insert high quality plots into ms word/powerpoint and you are willing to use inkscape as converter you can apply this solution:

from matplotlib import pyplot as plt
import subprocess, os

def plot_as_emf(figure, **kwargs):
    inkscape_path = kwargs.get('inkscape', "C://Program Files//Inkscape//inkscape.exe")
    filepath = kwargs.get('filename', None)

    if filepath is not None:
        path, filename = os.path.split(filepath)
        filename, extension = os.path.splitext(filename)

        svg_filepath = os.path.join(path, filename+'.svg')
        emf_filepath = os.path.join(path, filename+'.emf')

        figure.savefig(svg_filepath, format='svg')

        subprocess.call([inkscape_path, svg_filepath, '--export-emf', emf_filepath])
        os.remove(svg_filepath)

In order to test this function you can run a simple example:

plt.plot([1,2], [4,5])
fig = plt.gcf()
plot_as_emf(fig, filename="C:/test.emf")

Comments

5

Can try for svg format:

plt.savefig("filepath.svg", format = 'svg', dpi=300)

Comments

1

Try exporting as a pdf or svg as described in http://neuroscience.telenczuk.pl/?p=331 If you need an eps the pdf2ps command works great.

2 Comments

That website has all broken image links at the moment.
I used waybackmachine to get the previous snapshots: web.archive.org/web/20230205040301/https://…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.