exporting svg image from matlab surface plot - matlab

I need some to produce some publication-quality figures. I first export the figures from matlab in .svg format, and then I do some post-processing in inkscape. I am no problem with figures generated using plot or scatter, but when I export figures generated using surf (in view(2)), I run into problems. If I use plot or scatter, I am able to ungroup and process various parts like the title, axes, scattered points, lines, etc. in inkscape. For surf, however, matlab just exports one single figures with all various parts grouped into one single unit. I can't separate individual part, and when I zoom very close I can actually see the bitmap resolution for the axes and titles (if I use plot, the titles and axes have 'infinite' resolution when I zoom very close). I am fine with the surface plot having finite resolution, but I need to at least be able to process the axes and titles (which I currently cannot do). What should I do so that I can 'separate' the title and axes from the main plot, just like figures generated from plot and scatter?

I stumbled across this question, since I encountered the same problem.
As mentioned by #vindarmagnus, it is possible to use tikz and get rather nice results. However, tikz experiences problems with large data sets in my experience as present when using surf etc..
Solution, that worked for me:
Change the renderer to painters and the exported .svg file will retain its vectorgraphic properties when opened e.g. in inkscape:
figure('Renderer','Painters');

I used to use Inkscape for my scientific publications as well, but I found that a lot of the time you can get better results with pgfplots in latex, together with the matlab2tikz matlabscript. There’s a ton of resources about this online, but here’s how my workflow would look adopted to your surf situation. I have macOSX with latex, matlab and matlab2tikz installed. Will work with little to no modifications on linux.
In Matlab:
surf(peaks(25))
matlab2tikz('plot.tikz’)
Then I have the following bash-script (just a script in the same folder as the image, which is executed by mere double-click). (Needs to be chmod-ed as an executable for that).
#!/bin/bash
cd ~/Desktop
rm *.eps
cat > plot.tex << EOF
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{max space between ticks=50}
\pgfplotsset{scaled ticks = false}
\pgfplotsset{compat=1.6}
\pgfplotsset{xticklabel style={/pgf/number format/fixed}}
\pgfplotsset{yticklabel style={/pgf/number format/fixed}}
\begin{document}
\input{plot.tikz}
\end{document}
EOF
pdflatex plot.tex
pdf2ps plot.pdf
ps2eps plot.ps
Note that the row cd ~/Desktop above should be changed as to reflect which folder the script is supposed to be run from (a bit crappy, but needed since Finder doesn’t properly pass along the folder from a program is executed, afaik).
This yields high-quality images in eps or pdf or what you like, with a ton of settings for axes and ticks etc. And it all uses native latex fonts.
Edit:
Recently I’ve begun to use patch() in matlab and then export it to tikz in the same manner as above, with great results. That’s another suggestion!

You can use also:
set(gcf,'Renderer','Painters')

Related

Copy figure in Matlab

I am asking for a solution to the blurred image I get when I use copy figure option in Matlab. For example, when I plot 3d figure in matlab I get the following image (It is the result of printscreen!)..
However, when I use the option copy figure I get a blurred image as the following image
.
How can I avoid this as I need to get high resolution image using copy figure option. Thank you in advance for any help you may lend.
By default, the figure is copied as an Enhanced Metafile (a color graphics format) which works for basic plots such as bar plots, line plots, and other 2D plots but may yield undesirable results for more complex plots as in your case. This is because the rendering of the Metafile content is controlled my Microsoft Word and it may render things differently than MATLAB.
If you need a high resolution image, I would recommend using the print command where you can specify the desired resolution (using the -r option). Then you can import the resulting image into Word
print(gcf, 'myfile.png', '-dpng', '-r300')
Alternately, you can use export_fig from the File Exchange which better preserves the appearance
Another option would be to change the format used by Copy Figure to tell it to use a bitmap instead
Or you can call Copy Figure programmatically and specify a different format (either a bitmap or PDF)
print(gcf, '-clipboard', '-dpdf')

Matlab figure saved as .eps appears in 'horizontal sections' when opened in Adobe Illustrator

I normally save my matlab figures as .eps and then make them better looking using Adobe Illustrator. This works for most figures but not all.
For example I plotted my data using the violin.m function from file exchange. When I save it as .eps and open it in Illustrator, I don't get an editable figure as I usually do. Instead, My figure appears chopped in horizontal sections, and all I can do is delete them (like in the figure below).
Is there anything I can do in either matlab or Illustrator to be able to edit the figure?
I don't need an .eps file, I just need to be able to edit it.
EDITS:
I tried #MattSchmatt's suggestion of using the print2eps function but I had the same problem.
Saving as .pdf doesn't solve the problem, because the image I get is not editable in Illustrator (plus, I also get the horizontal 'chunks').
A minimal, complete and verifiable example requires matlab, the violin function linked above and illustrator. But if it helps, here's the matlab code to produce a similar figure. I save by clicking on Figure -> save as. (But as I said above I tried the print2eps function and that was the same).
X = rand(100,6);
figure; violin(X)
I tried the following, didn't work either.
set(gcf, 'Renderer', 'painters')
As the author of the question suggests, I also wasn't able to export an editable (vectorized) .eps file into Illustrator. However, exporting it as .pdf does the trick. The 'fill' for the violin plots is weird, though, and has horizontal sections (perhaps something to do with the way the density estimate is being plotted?) all over. I was able to fix this and make the plots normal (with solid filled shades): select all the horizontal sections/chunks using the magic wand tool, and then increase opacity to 100% (it is set to 50 based on the exported file). Once all the required edits are made, export the file as .tiff and it seems to look fine. Hope this replicates, and thus helps! (my MATLAB version is 2016, and Illustrator version is the CS5)

MatLab Eps Print Webdings interpreter

So recently I decided to not use the standard markers that Matlab provides and use my own via: a set of fonts including Webdings or WingDings. I make a standard scatter plot, and plot the text (in wingdings), using the text command, over the locations of the markers.
When I save these plots as png files, they print perfectly. They also appear perfectly on my screen after plotting.
However when I save these plots as eps files, the webdings are turning into their original letters 'l' or 'w'. It also looks like it's plotting them in a Courier font, but my default is Helvetica.
I've read through this previous post, but my question differs in that he is looking to use Latex as the interpreter and to include fonts, whereas I don't want to use Latex as the interpreter. However, it seems like my default interpreter (not sure what that is), isn't doing the job when converting to EPS.
Example:
imageR='w'
text(xf2,yf2,imageR,'fontName',font,'FontSize',fontR,'HorizontalAl','left','color','w')
I figured it out....took me way too long.
Ghostscript / Postscript only export with a few fonts when you are exporting eps files.
This link was incredibly helpful.
I just switched from wingdings/webdings to ZapfDinbats...Practically the same thing...

Exporting figures as vector graphics in .pdf-format using HG2-Update and 'painters' renderer is not working properly

I'm using the still undocumented HG2-Update to create my MATLAB plots, because they just look that much nicer.
(Source: Yair Altman)
Actually, using the current version Release 2013b it works quite nicely and there are not much issues. Except one wants to export the figures as vector graphics (renderer: '-painters'), especially as pdf.
I use the commands:
saveas(gcf,'test.pdf','pdf')
or
print(gcf,'test.pdf','-dpdf')
There are rendering issues, the print does not contain the whole figure and some parts are cropped or non-default fonts are not recognized.
But I'd really like to stay with HG2 and I'd still like to use vector graphics. Is there any solution or workaround?
Exporting vector graphics using the yet not official HG2-Update is quite an issue. The .pdf-export is still totally screwed up.
What is working fine is the .svg-export, apart from that the boundary box is not set properly.
The long workaround would be:
Save the plot with '-dsvg' (print-command) or 'svg' (saveas-command) as vector graphic, open the file in the open source application Inkscape and save again as .pdf with the Export area is drawing checkmark set.
Quite complicated, so I found a way to do it via command-line directly from Matlab (Inkscape still required!):
filename = 'test';
inkscapepath = '"C:\Program Files (x86)\Inkscape\inkscape.exe"';
%// save as .svg
saveas(gcf,filename,'svg')
%// open and save with "export-area-drawing" set via command line
system( [inkscapepath ' ' filename ...
'.svg --export-area-drawing --export-pdf=' filename '.pdf'])
It takes some time, but works without any known issues for now.
Additionally delete the svg-File afterwards:
delete([filename '.svg'])
I had the same problem and used the workaround from thewaywewalk. Now I discovered the MATLAB function "hgexport" works under HG2 (in R2014a).
An issue still was the paper size. I want to use the same size for all graphs with as little white frame as possible. Here you have to set two sizes:
The papersize is set with set(gcf,'PaperSize',[width height]) and the size of your chart is set through export styles. These are set in "Export Setup" or command line:
exp_style=hgexport('readstyle','default');
exp_style.Width = 'width';
exp_style.Height = 'height';
exp_style.Renderer = 'painters';
Now you can export your pdf:
hgexport(gcf,'pdfname',exp_style,'Format','pdf');
PS: In HG2 you may also use Latex for tick labels:
set(gca,'TickLabelInterpreter','latex');

MATLAB and high quality EPS figures

I am looking to export my MATLAB plot as a high quality figure. Specifically, I would like to save it as a vector based file format such as EPS or SVG.
I have tried print and saveas commands:
saveas(h,'myFileName','epsc2');
print('-r150','-depsc2', 'myFilename');
On all occasions this produces poor quality parts of the graph, although the axis-labels are indeed vector. Why does MatLab do some horrible rendering before putting it into an EPS?
Example of poor quality plot here:
http://users.ox.ac.uk/~pemb2372/myFileName.eps
Edit:
It is also worth noting that if you use a Mac viewing an EPS file from Matlab, 'Preview' app may render inner graph content rasterized and poor quality, while leaving the axis and labels vectorized and high quality. This is very misleading but when you open said EPS file in, for example, Inkscape, the quality is actually vector and quite high.
Edit 2:
My university hosting account has expired, so you can no longer view the figure. Suffice it to say that it showed a poor quality raster-style plot within high quality beautiful axis lines, ticks and labels.
I thought I would share the issue I had, and how I overcame it...
I was getting terrible results because I had the wrong renderer set to default. In my startup.m, I had the zbuffer renderer enabled. This is an example eps output.
I made that eps output with: print(gcf,'-depsc2','filename.eps'). This eps is so OBVIOUSLY rasterised. It makes me angry at matlab. Then, I had a brainwave - perhaps my default renderer zbuffer is interfering with the image save process. So, adding the line:
set(gcf,'renderer','painters')
and running the print command as before, here is the output:
Note that I just took screenshots of the eps output files at 100%. And I can confirm the second image is actually vector. Here is a good question/explanation on choosing Renderers in MATLAB.
Matlab can export to pdf with better quality than EPS, but with its own caveats of setting decent margins and font sizes.
edit:
Examples are similar to the EPS case as explained in the help of e.g. print:
saveas(gcf,'filename.pdf')
or
print('-dpdf','filename.pdf')
You might also want to take a look at the PaperSize, PaperPosition and PaperUnits properties of your figure (by means of the set and get functions).
edit: Another option is to use one of the functions available on FileExchange such as the ones mentioned by #user664303 below. My personal favorite for use with LaTeX is matlab2tikz for which the latest version can be gotten from GitHub. Together with the external library of TikZ, I think this delivers some of the most nicest graphs around.
Probably it's also best to mention that I have been actively involved in the matlab2tikz project since 2012.
The export_fig function on the MATLAB file exchange is a reasonably reliable way of accurately exporting figures to eps and pdf (as well as bitmap formats) in MATLAB.
The plot2svg function, also from the file exchange, allows you to export in svg format. It provides some additional benefits, such as being able to export translucent patch objects in vector format.
A comparison of exporting methods is given in this blog post.
I always acquire the final plots (those which are supposed to be inserted into papers and publications) by matplotlib library of python.
You can bet on the amazing quality of the generated plots, both .pdf and .eps formats.