Jul 21, 2009

Produce Print-Quality Figures from Matlab

This post explains how to create print-quality figures in Matlab. The process is efficient and results in editable vector-graphics that you can include in any document including Microsoft Word, Open Office, or latex. Before you create your figures you might also want to check my article on combinations of linestyles and markers to have clear plots in black-and-white and color.

Introduction

There are serveral ways to export figures from matlab for a publication. In Windows, in the figure menu in matlab you choose to copy the figure to clipboard and then edit it in powerpoint or word any figure property you like. After that you paste them to your word document. On Linux you would usually export to EPS to get highest quality images.

EPS offers potentially the best quality images, however it brings problems especially with font size, figure size, and transparency. Increasing font sizes might overlay multiple layers of the same text. The bounding box is often messed up and you might want transparency. Laprint, an export tool for matlab, is a work-around for the font size problem, however legends do not get exported correctly and the figures still miss transparency. In this post I discuss a way to produce high-quality figures avoiding all those problems.

One way around the problems with Matlab is not using Matlab. GNU R produces very high quality figures, however R also has its own limitations. How to get the right font size and transparency with Matlab? Don't worry, there is a good solution that gives high quality figures.

SVG

I just rediscovered the SVG format. It is really easy to retouch EPS images or import SVGs. SVG, short for scalable vector graphics, is a XML-based file format for two-dimensional vector graphics. This makes it really easy to read the files and edit them from the command line using sed, grep, awk, etc.

Why should you learn to use a vector format? Because the quality is very high!

If you export your matlab figure to EPS the problem is then to edit the image files. If you edit them with a raster editor, for example the gimp the quality gets completely lost. In contrast if you export from matlab to SVG, you edit your SVG file with inkscape or one of the other editors, similar to any desktop publishing tool. You can directly change line attributes, change text, and change the bounding box very comfortably.

There are free vector graphic editors with functionality similar to Adobe InDesign such as inkscape, and many other tools to edit and create SVGs, including OpenOffice, Xfig, scribus. gnuplot. Konqueror, Firefox, and, since recently, Internet Explorer can display SVGs. Editing of SVGs in Inkscape offers all comforts of a modern desktop publishing tool. See this Inkscape tutorial for more info about editing.

SVG in Matlab

For matlab, there is the package plot2svg, which allows you to export figures from matlab to SVG. In order to export your figure instead of exportfig(1,'filename.eps','width',5,'color','rgb'); you do plot2svg('filename.svg',1).

There are small caveats to using inkscape that bring complications, which however can be easily overcome. If you export directly with inkscape to eps, transparent colors might get black, you don't see anything. This also occurs when exporting to Latex (pdftricks).

There is a solution: convert the edited SVG first to PDF and then convert from pdf to eps using e.g. ImageMagick. You can integrate eps figures with most word processors and it would be the default image format with latex (texi2dvi).

Breakdown so far:
  1. (matlab) export to svg
    >> plot2svg('filename.svg',1)
  2. edit in svg editor (e.g. inkscape), export to pdf
    inkscape -F filename.svg --export-pdf=filename.pdf
  3. convert pdf to eps
    convert filename.pdf filename.eps
This doesn't help with the font size problem yet. But you can automate the editing and conversion process, which reduces the problem considerably.
  1. Change font size: sed -i "/font-size/s/[0-9][0-9]*pt/14pt/g" filename.svg. In this case, you would change your font size to 11pt.
  2. Convert from SVG to pdf: inkscape -f filename.svg --export-pdf=filename.pdf
  3. Convert from pdf to eps: convert filename.pdf filename.eps
Using this method, you'll have nearly same comfort as with matlab's copy-figure function in windows and get figures of much higher quality because of the vector format.

Alternative EPS Export

In a comment to my post LaTex Poster Template, Marcus vLW pointed out a method for exporting to EPS from matlab, which impressed me a lot, so I want to reshare his method here. It seems to give proper transparency. h=figure('position',[100 0 1100 1100],'paperpositionmode','auto', 'color','none','InvertHardcopy','off');
% plot something...
print(h,'-depsc2','-painters','plotname.eps')
Enjoy. Please vote this post up if you liked it and leave a comment below for questions and suggestions. 

10 comments:

  1. That's one way to do it. I just print my figures directly to PDF, and they stay vector in that case.

    ReplyDelete
  2. @Anonymous: The question is if you can edit your files comfortably. While SVG files are easy to edit, for pdf files I yet have to see a free editor (I am excluding adobe's pdf editors for the windows platform) that gives you the same comfort as you have with inkscape and the likes.

    ReplyDelete
  3. @Anonymous: If you include transparency in any of your graphics in MATLAB, exporting to PDF will be done with the bitmap-based OpenGL renderer. In such situations, SVG appears to be the only option to export transparent vector graphics out of MATLAB.

    However, if you don't need transparency, MATLAB will allow you to adjust almost any of the graphics properties (line size, width, font size, font face, paper size, etc.) using Figure Properties and Axes Properties. The interface is a bit klunky, but there should be very little reason to use a third party editor on MATLAB graphics.

    ReplyDelete
  4. @Anonymous: Thanks for your great explanation. I find transparency very important for figures, but I this is not the only reason I find SVG a great format for matlab export.

    I usually run matlab without java (needed for the figure editor), often in remote, and I don't want to fire up matlab every time I want to make changes to a figure. Inkscape is a very nice editor (not klunky at all, I think) and further the advantage of having a text-based format like SVG is that it allows application of regular expressions and many command line commands to automatically change many properties at once and even of many figures at the same time if necessary. This makes it very fast to get figures exactly the way you want them.

    ReplyDelete
  5. Well I tried similar procedure on some wmf i.e. edited with Inkscape exported to pdf and than used the convert from ImageMagick tool. The result was dissapointing since all the lines were blurred - has anyone idea why that happend?

    ReplyDelete
  6. @Anonymous: I suppose you convert from windows meta file (wmf) to pdf to eps. .wmfs can contain both vector and raster elements, so I am not sure how the inkscape wmf import works, but I assume that in inkscape the image doesn't look blurred. Have you looked at the pdf, does it look alright?

    ReplyDelete
  7. Hi, I have a problem with plot2svg,
    when I magnify my plot in matlab(zoom in) and use the command plot2svg, still saves the same small figure from matlab, and in svg magnifying it to the degree that I need does not contain the details. how can I convert the zoomed in image to svg?

    ReplyDelete
  8. Hi guys, when I am actually willing of reading these blog post to be updated regularly. It offers fastidious stuff.
    cheap brochure printing

    ReplyDelete
  9. You're so cool! I do not believe I've truly read through a single thing like that before.
    So great to discover somebody with a few unique thoughts on this
    topic. Seriously.. many thanks for starting this up. This web
    site is something that's needed on the web, someone with
    a little originality!

    Also visit my site - car repair spring hill

    ReplyDelete