Converting text in Matplotlib when exporting .eps files
Asked Answered
T

1

12

I'd like to be able to save Matplotlib plots and add them directly as vector graphics in Microsoft Word documents. However, the only format supported by Word and Matplotlib both is .eps, and the axis text is completely missing in Word if I try. I'll show you:

Here's a minimal working example script:

import matplotlib.pyplot as plt
import numpy as np

axes = plt.gca()
data = np.random.random((2, 100))
axes.plot(data[0, :], data[1, :])

Apparently, the way that Matplotlib saves text in .eps files is incompatible with the way that Word reads text from .eps files. The exported .eps files look fine in PS_View.

I can think of two workarounds, but I don't know how to implement them or if it is at all possible in Matplotlib:

  1. Vectorise the text so that it is embedded as paths. This is supported by Matplotlib's SVG backend by setting the rcParam 'svg.fonttype' to 'path', but it doesn't seem directly supported by the ps backend. This would be the ideal workaround. Is there any way to do this?
  2. Rasterise only the text when exporting as .eps. This would be a less ideal workaround. Can this be done?
Taal answered 26/6, 2014 at 8:1 Comment(8)
I've just tried embedding an .eps file in the latest Microsoft Word (2016) on Mac and it now appears to display the text correctly.Pendent
Thanks for testing, @mfitzp! Since posting this question my company has upgraded from Word 2010 to Word 2013. I decided to try again, but the problem still remains here. Either the issue got fixed in Word 2016, or the Mac version of Word is just better. Nevertheless, my questions about the workarounds (questions that are not platform-specific) still stand.Taal
I can confirm it also doesn't work on Word 2013 on Windows at least (can't test 2016 unfortunately).Pendent
How are you saving the image? are you using savefig()?Claus
@wesanyer: I was saving the picture using the plot GUI; I believe with the QT4Agg user interface. I suppose the GUI must call savefig() to actually save the image, though.Taal
For Microsoft Word 2010 and 2013 I save the figure as .svg and later I convert it to .emf using a free software called Inkscape.Blanchette
Does it now have a solution?Madly
@cqcn1991 Yes, the accepted answer is a fully functional workaround, although it also requires using Inkscape in order to export to a file type that Word can handle well.Taal
A
3

As sebacastroh points out, one can save the matplotlib figure as svg using plt.savefig() and then use Inkscape to do the conversion between svg and emf. Enhanced Meta files (emf) are easily read by any Office programm.
This can be automated, like so

import matplotlib.pyplot as plt
import numpy as np
from subprocess import call

def saveEMF(filename):
    path_to_inkscape = "D:\Path\to\Inkscape\inkscape.exe"
    call([path_to_inkscape, "--file", filename,  "--export-emf",  filename[:-4]+".emf" ])

axes = plt.gca()
data = np.random.random((2, 100))
axes.plot(data[0, :], data[1, :])
plt.title("some title")
plt.xlabel(u"some x label [µm]")
plt.ylabel("some y label")

fn = "data.svg"
plt.savefig(fn)
saveEMF(fn)

It may also make sense to save the saveEMF() function externally in a module to always have it at hand.

Assured answered 25/10, 2016 at 22:20 Comment(2)
While it's a bit of a hacky workaround to convert from .svg to .emf using Inkscape, your script is pretty convenient, and I can confirm that it works perfectly. Thanks!Taal
In Inkscape 1.0, had to use --export-filename=foo.emfDecimal

© 2022 - 2024 — McMap. All rights reserved.