# Smooth MATLAB Graphics11

Posted by Jiro Doke,

Jiro's pick this week is MYAA by Anders Brun.   A few months ago, I wrote a post for Loren's Art of MATLAB blog about creating publication-quality graphics. That post was about controlling various properties of graphics objects to make the visualization a higher quality. Another way of making better-looking graphics is to use anti-aliasing. Anti-aliasing provides a smoother look to the lines drawn on the screen. PCs use anti-aliasing to make the fonts smoother. With MYAA, you can easily anti-alias your MATLAB graphics. This is great for both on-screen presentation as well as MATLAB publishing (this blog post is a published document). Original Plot With the regular screen resolution, you can see the aliasing. It is most apparent with diagonal lines.
membrane
Anti-Aliased Plot Anti-aliased plot looks much smoother. MYAA supersamples the plot (as an image) and then resizes it (via a filtering method), giving an anti-aliased look.
myaa
If we take a closer look at the two images, you can see the anti-aliasing effect.
im1 = imread('antialias_01.png');
subplot(1,2,1);
imshow(im1(100:150, 300:350, :));
title('Original');
subplot(1,2,2);
imshow(im2(100:150, 300:350, :));
title('Anti-Aliased');
More Features MYAA has additional features that make it a very handy tool. For example, you can specify the supersampling factor to provide an even smoother look. The tool also has a nice interactive feature where you can refresh the graphics (after zooming or rotating the original plot) or zoom in and out with the anti-aliased plot. Finally, I like how Anders has been quickly updating his entry in response to the user feedback. He posted this file early this month, and he has already updated several times, making this tool more user-friendly. Comments Do you ever make presentations directly from MATLAB? What are some tricks you employ to make MATLAB a presentation tool? As a starter, I once created a MATLAB animation player for displaying animations. Tell us about your stories here.

Get the MATLAB code Published with MATLAB® 7.6

Quan replied on : 1 of 11

Not to be a troll, but I kind of like the way the original plot looks better. Nethertheless, a very cool tool!

In terms of creating presentations, I’ve written some scripts that create plots and then automatically copies and pastes them into a powerpoint presentation. It is really useful when you’re processing a large batch of data!

Daniel Armyr replied on : 2 of 11

Hi.

This post raises a question with me: Does matlab use hardware accellerated graphics? And if so, why not allow hardware-accelerated anti-aliasing?

jiro replied on : 3 of 11

@Daniel,
We do use the hardware, but we don’t expose the supersampling feature through that path, so if you want supersampling, for now you’ll need to do it in software.

@Quan,
Thanks for your comments. Yes, functions for automatically pasting MATLAB plots into a PowerPoint presentation can be very useful. In fact, now you can do this by using MATLAB’s publish capability and selecting PPT as the output format.

Ad replied on : 4 of 11

Hi,

I just found a tip for anti-ailiasing lines. Just add the attribute ‘LineSmoothing’,’on’. i.e.

line([x0n x1n],[y0n y1n],’LineWidth’,2,’LineSmoothing’,’on’);

It doesn’t appear to be documented in matlab but it works nicely.

Jody Klymak replied on : 5 of 11

Sorry to revive an old post, but another post made me think to look and see if this had been treated before.

My number-1 beef with Matlab is the quality of the display graphics. I don’t understand why

>> plot(1:10,’linewi’,1)
>> hold on
>> plot((1:10)+1,’linewi’,0.5)

look exactly the same on the screen despite looking quite different in anything sent to print. Is there a reason for this limitation in this day and age, or do I have something set inappropriately?

jiro replied on : 6 of 11

You probably don’t see any difference between the lines on the screen, because MATLAB graphics currently don’t support real antialiasing.

For example, a typical screen resolution is 96 pixels per inch. A point is 1/72th of an inch. This means that the 1 point line is 1 1/3 pixels, while the 0.5 point line is 2/3 pixels. So both lines get rounded to 1 pixel.

However, I have relayed your comment over to development.

nisa replied on : 7 of 11

Hi,

I have just started to learn to use mathlab can this myaa codes be used with mathlab 7.5.0(R2007b)using Psychtoolbox?thanks.

jiro replied on : 8 of 11

Nisa,

I’m not familiar with the Psychtoolbox. It’s not a toolbox developed by the MathWorks. As far as incorporating “myaa” code in R2007b, that should not be a problem, but you would have to do the incorporation yourself.

Nicolas replied on : 9 of 11

Nice, but gives funny results with latex output. My code is:

[L,Obj] = legend(‘$\overline{h}$’);
t = findobj(Obj,’Type’,’Text’);
set(t,’Interpreter’,’latex’);
myaa;

Markus Kuhn replied on : 10 of 11

Doing anti-aliasing in MATLAB is usually the wrong approach for publication-quality figures. Instead, use MATLAB to produce a resolution-independent vector graphics format (EPS, PDF, SVG, etc.), and let the software that displays or prints that worry about anti-aliasing. Some of these applications (e.g., Adobe Reader, printer driver) have very good anti-aliasing algorithms and adjust them to the specific properties of your display or output medium (e.g. LCD or Bayer mosaic subpixel rendering).

jiro replied on : 11 of 11

@Markus,

Yes, I agree that for creating a publication-quality output file, using a vector graphics format is the better approach. This tool is more for if you were doing on-screen presentation directly from MATLAB or making use of the auto-publishing feature.