9

I normally save my plots in python as PNGs. That works fine, but has the obvious drawbacks when it comes to rescaling afterwards. Therefore I want to save my plots, figures etc. as vector graphics and then be able to import them into windows applications such as word or powerpoint.

I now face several problems.

  • saving something from matplotlib as support vector graphics (SVG) works fine. Problem, I cannot (easily) import it into word or pp.
  • saving as EPS gives a horrible result. For example I have shaded areas between two lines (using fill_between with an alpha of 0.3) and in EPS these areas are fully coloured. Import into word/pp works but the graphics are no good.
  • I also tried to convert SVG files to EPS using inkscape. Here the shaded areas are still retained but the overall quality is again no good (looks actually more like a bad raster graphic) and also some legends are cut off.

Here are some images trying to visualise my problem.

plot saved as PNG after importing into word - the way it should look

plot saved as EPS after importing into word - no shading, axe labels and no legend (not shown)

plot saved as SVG and then converted to EPS using inkscape after importing into word - shading correct, but all writing (labels, title etc.) in poor quality, looks actually more like a bad raster graphic

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
red_tiger
  • 1,402
  • 3
  • 16
  • 32
  • You can try converting the vector graphic into `wmf` or `emf` format. The latter two are vector graphic format natively supported by Office. – Siyuan Ren Mar 05 '15 at 02:39
  • It might help if you provide the `savefig` command. – Alf Apr 17 '18 at 07:12
  • 1
    For anyone reading this in 2020 or later, EPS has become a total non-starter. There used to be a way to prevent Office from converting it to EMF via registry setting, but as of a year or three ago, Office apps on Windows will no longer import EPS. On the other hand, recent versions of PPT will import SVG, so that's how I'd try to solve the problem nowadays. – Steve Rindsberg Jul 17 '21 at 15:11

3 Answers3

1

Windows now seems to provide good support for embedding most PDFs as vector files, and will preserve vector characteristics when you export to PDF. This is based on my experience using Office 2011 for Mac, but should extend to MS Office 2007 for Windows. I save my matplotlib plot as a PDF, import it to a Word document, and am able to save it as a vector output.

The one catch: if you embed a PDF inside a textbox, Word will rasterize this when you save it to a PDF.

For more advanced vector support, try OpenOffice- I believe it supports direct SVG import, though with some bugs.

emunsing
  • 9,536
  • 3
  • 23
  • 29
1

This is a duplicate answer to this question.

For anyone who still needs this, I wrote a basic function that can let you save a file as an emf from matplotlib, as long as you have inkscape installed.

import matplotlib.pyplot as plt
import subprocess
import os
inkscapePath = r"path\to\inkscape.exe"
savePath= r"path\to\images\folder"

def exportEmf(savePath, plotName, fig=None, keepSVG=False):
    """Save a figure as an emf file

    Parameters
    ----------
    savePath : str, the path to the directory you want the image saved in
    plotName : str, the name of the image 
    fig : matplotlib figure, (optional, default uses gca)
    keepSVG : bool, whether to keep the interim svg file
    """

    figFolder = savePath + r"\{}.{}"
    svgFile = figFolder.format(plotName,"svg")
    emfFile = figFolder.format(plotName,"emf")
    if fig:
        use=fig
    else:
        use=plt
    use.savefig(svgFile)
    subprocess.run([inkscapePath, svgFile, '-M', emfFile])
 
    if not keepSVG:
        os.system('del "{}"'.format(svgFile))

#Example Usage

import numpy as np
tt = np.linspace(0, 2*3.14159)
plt.plot(tt, np.sin(tt))
exportEmf(r"C:\Users\userName", 'FileName')
imbr
  • 6,226
  • 4
  • 53
  • 65
Gilly Gumption
  • 111
  • 1
  • 7
0

What version of Office are you using and on Windows or Mac?

And more generally: Using EPS with Office is a peculiar business. Office doesn't handle EPS in any standard way. If you will always print to a PostScript printer or use some other PostScript-based output method (printing to PS then distilling to PDF, for example) EPS can work very well indeed if you make a registry change to tell PPT (don't know about Word) to behave properly. Ask and I'll see if I can dig up the details. Otherwise, it tries to interpret the PS content of the EPS itself and may or may not make a good job of it.

Can you convert to EMF (assuming Windows here)? And if not, can you control the resolution of your images? You may have best results by converting to a higher resolution PNG.

Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34
  • Thanks for your answer. I work on Windows with Office 2007. I am not dependant on using EPS, I rather want to have a vector graphic that can be used in Word and or Powerpoint (where Powerpoint in the end is not so important, as you normally don't need to print this). EMF I have not tried, but can do. Higher resolution PNGs can help but they also blow up the document. What seemed to work was generating PDFs, but handling them in Word is not so easy and I am not sure if the PDF generated in Python is a real vector graphic? – red_tiger Sep 19 '14 at 07:46
  • For the kind of graphics you showed as examples, PNG's compression should work quite well. It's worth a try, in any case. Oddly, PowerPoint (and possibly Word) are sometimes more responsive with large PNGs than with vector graphics that contain large numbers of line segments or curve nodes (as yours will). – Steve Rindsberg Sep 19 '14 at 12:59