0

I'm currently writing a scientific paper and am generating most of the figures using matplotlib. I have a pipeline set up using a makefile that regenerates all of my plots whenever I update the data. My problem is that the figures are made up multiple panels, and some of those panels should contain vector illustrations which I've created using Adobe Illustrator. How can I automatically combine the graphs with the illustrations when I update my raw data? I could save the vector illustrations in a raster format and then display them using matplotlib's imshow function, but I want the output to be a vector to ensure the best possible print quality.

R_Beagrie
  • 583
  • 5
  • 10
  • 1
    Have you tried setting your backend to SVG so that all of your images are in vector format? – MattDMo Nov 05 '14 at 18:22
  • Matplotlib does not have the ability to import SVG images. – tom10 Nov 05 '14 at 21:10
  • @MattDMo yes, I can export the parts made with matplotlib as SVG, but then I need another program to combine them with the illustrator images - so the question would be what program can I use for this purpose? I could combine the images together using matplotlib itself, but as tom10 noted, matplotlib cannot import any SVG images I made using illustrator. – R_Beagrie Nov 06 '14 at 09:41
  • @R_Beagrie Illustrator should be able to import SVGs (you may need a plugin, I don't remember). There are also a bunch of free SVG editing programs available online, depending on your platform. Inkscape for Windows is pretty decent. – MattDMo Nov 06 '14 at 15:40

2 Answers2

0

After some more extensive googling I found this old message on the matplotlib mailing list:

The thread suggests using the python library PyX, which works well for me.

I can save both the illustrator diagrams and the matplotlib plots as .eps files, and then combine them together like this:

import pyx
c = pyx.canvas.canvas()
c.insert(pyx.epsfile.epsfile(0, 0, "1.eps", align="tl"))
c.insert(pyx.epsfile.epsfile(0,0,"2.eps", align="tr"))
c.writeEPSfile("combined.eps")
R_Beagrie
  • 583
  • 5
  • 10
0

I found this example in the svgutils documentation which outlines how to combine matplotlib-generated SVGs into a single plot.

Here's the example from that page:

import svgutils.transform as sg
import sys 

#create new SVG figure
fig = sg.SVGFigure("16cm", "6.5cm")

# load matpotlib-generated figures
fig1 = sg.fromfile('sigmoid_fit.svg')
fig2 = sg.fromfile('anscombe.svg')

# get the plot objects
plot1 = fig1.getroot()
plot2 = fig2.getroot()
plot2.moveto(280, 0, scale=0.5)

# add text labels
txt1 = sg.TextElement(25,20, "A", size=12, weight="bold")
txt2 = sg.TextElement(305,20, "B", size=12, weight="bold")

# append plots and labels to figure
fig.append([plot1, plot2])
fig.append([txt1, txt2])

# save generated SVG files
fig.save("fig_final.svg")
Jeff
  • 29
  • 3