Getting rid of interpolation/aliasing in EPS export of matlab? - matlab

I have a 2D color-map plot created with imagesc and want to export it as a .eps file using
print -depsc.
The problem is that the "original" image data is from a rather small matrix (131 x 131). When I view the image in the matlab figure window, I can see all the individual pixels if I zoom a bit closer.
When I export to eps, however, there seems to be some interpolation or anti-aliasing going on, in that neighboring pixels get blurred/blended into each other. I don't get the problem if I export a high-resolution tiff, but that format is not an option (as demanded by a publisher).
How can I obtain an eps that preserves the pixely structure of my image without applying interpolation or anti-aliasing?

The blurring actually depends on the rendering software your viewer application or printer uses. To get good results all the time, make each pixel in your image an 8x8 block of pixels of the same color. The blurring then only affects the pixels at the edge of each block. 8x8 blocks are best as they compress without nasty artifacts using DCT compression (sometimes used in eps files).

Old question, but highly ranked in Google, so here is my answer:
Open the .eps-file with a text editor, search for "Interpolate" and change the following "true" to "false". Repeat that step for all Interpolate-statements.
It might also depend on the viewer you're using, but probably just because some viewers ignore the "Interpolate"s...
Had the same problem using plot2svg in Matlab and exporting from Inkscape to eps.

Related

Change size of figure in MATLAB to use them in LaTeX

I have generated several plots in MATLAB and I haven't set the ( width or height) to any of them. I save them as pdf to place them in LaTeX but each figure appear with different size. I can change each figure size in LaTeX but I wonder if there is an easy way in MATLAB to get same size for all figures.
Can I reset the figure size after generating in MATLAB or I have to regenerate each figure with specific size?
What is the best way to export figures in MATLAB to use them in LaTeX?
Exporting figures from MATLAB to LaTeX is best done via .eps or directly to PDF. MATLAB's saveas() functionality supports both. The benefit of these file-types is that thy are vector files, meaning, loosely, that coordinates of corners in the lines are saved, as opposed to a bunch of fixed pixels such as with PNG or JPG figures. Using then these figures in LaTeX gives infinite scalability (theoretically, usefulness of highly-zoomed figures may vary).
If you need different aspect ratios, i.e. one figure taller than broad, the other broader than tall, You'd have to set the proper aspect ratio for each figure independently, then save using one of the aforementioned vector file types.
One of the best ways to export pretty figures is the export_fig() functionality written by Oliver Woodford and currently maintained by Yair Altman and freely available on the FileExchange.
Answering your first question: yes you can resize figures after generating them. Either by hand (change the window size), or programatically: you can make a figure handle (h=figure();, where h is your handle), which has several options to control figure size; h.position being the most prominent: [upper-left, lower-left, upper-right, lower-right] being its content in terms of corner coordinates. Changing those changes the figure size.

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')

exporting figure to eps file

I am trying to export a figure which includes highlighting of regions - something like this: Highlight parts of matlab plot. Unfortunately, when I export the figures to .eps files the size is of the order of ~10 MB... Thus, when I include them in a tex file, the quality is severely degraded. As expected this problem seems to occur due to the use of the area function for the highlighting. Is there any workaround on this?
You have transparency in your plot which requires the renderer to be OpenGL which causes any EPS to not really be vector graphics but rather bitmaps coerced in some strange way. This is why the file size is much larger than what you would expect for vector graphics. If you open the resulting EPS file with an external editor (Illustrator, Inkscape, etc.) you will see what I am talking about. As a side-note, transparency isn't technically supported in EPS files.
Your options are really to
Save your figures as something else such as TIFF or PNG
You could try saving the figure as an EPS using export_fig from the file exchange but you will likely still have the same issue.
Turn off transparency, save to an EPS, and use Illustrator or an external program to change the transparency and try to save as an EPS file and see if you get better results.

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.

Saving MATLAB graphs in a specific resolution

How do I change the resolution of my MATLAB graphs? The default seems to be 96x96 PPI.
You can save using the print command; here's how:
h1=figure;
your plotting commands here
print(h1,'-djpeg','-r150','filename')
The -r150 argument sets the resolution to 150 DPI. you can set it higher or lower depending on your need. This is assuming you're saving as a JPEG. If not, there are other options such as -dpng for PNG, -dbmp for 24-bit bitmap, etc. more can be found in the help section for print.
That said, if your plot consists only of lines, I would recommend using -depsc which saves it as an EPS file, which is scalable and lossless. You also won't have to define a resolution, so you can simply do
print(h1,'-depsc','filename')
You can convert this EPS file to PDF and retain the same resolution, yet have portability between applications and platforms. I wouldn't recommend saving to PDF directly as MATLAB puts additional white spaces around the plot, wheras the EPS bounding box is tight, and looks neat.
Changing a Figure's Settings: Setting the resolution