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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With