Plot LineSmoothing property

I have already written about several undocumented hidden properties in the past. Today, I would like to introduce one of my favorites: the plot-line’s LineSmoothing property. Compare the following two outputs:

% Standard (non-smoothed) plot-line
plot(1:5,2:6,'o-', 'LineWidth',1);

Standard plot line (not anti-aliased)

Standard plot line (not anti-aliased)

% Smoothed (anti-aliased) plot line
plot(1:5,2:6,'o-', 'LineWidth',1, 'LineSmoothing','on');

Anti-aliased (smoothed) plot line

Anti-aliased (smoothed) plot line

Line smoothing (aka anti-aliasing) works by inserting semi-transparent pixels at the edges of the actual plot line, thereby giving an optical illusion of a smooth line without pixelization effects. In Matlab, antialiasing is done automatically for fonts, but unfortunately not for plot lines that have non-default line-widths.

Line-smoothing has been around for a long time. It was even mentioned in a user comment on the official MathWorks Pick-of-the-Week article that introduced the excellent Myaa utility (Myaa uses applicative Matlab code to create anti-aliased plot effects).

However, to this day (R2010a), the LineSmoothing property remains hidden, undocumented and not officially supported.

Perhaps the reason for this is the following bug: text objects that cross a smoothed line are obscured by it. Depending on the text size and the line width, the text might be completely hidden, although its handle indicates that it is visible and despite it being created after the plot line!

plot(1:5,2:6,'.-', 'LineWidth',5, 'LineSmoothing','on');
text(2.2,3.5, 'abcd','Color','r');

Smoothed line obscuring text

Smoothed line obscuring text

Note that this does not happen for standard (non-smoothed) lines:

plot(1:5,2:6,'.-', 'LineWidth',5);
text(2.2,3.5, 'abcd','Color','r');

Non-smoothed line does NOT obscure text

Non-smoothed line does NOT obscure text

Luckily, there’s a very simple workaround for this, that allows both line-smoothing and non-obstruction: simply set the text‘s z-position to a higher value than the plot line’s. In our example, we used a simple 2D plot line (i.e., z-position = 0), so let’s set z-position=1 for our text label:

plot(1:5,2:6,'.-', 'LineWidth',5, 'LineSmoothing','on');
text(2.2,3.5,1, 'abcd','Color','r');

Smoothed line not obscuring text

Smoothed line not obscuring text

MathWorks have developed a special workaround for the LineWidth problem in OpenGL, using

opengl('OpenGLLineSmoothingBug',1)

Unfortunately, as far as I could tell it has no visible effect in this particular case (perhaps I forgot to do something?).

One final note: the LineSmoothing property also exists for line, patch, surf, mesh and other similar objects.

Do you use have any other favorite undocumented/hidden property? If so, please share it in a comment below.

Related posts:

  1. Axes LooseInset property Matlab plot axes have an undocumented LooseInset property that sets empty margins around the axes, and can be set to provide a tighter fit of the axes to their surroundings....
  2. Plot LimInclude properties The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....
  3. Controlling plot data-tips Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....
  4. JIDE Property Grids The JIDE components pre-bundled in Matlab enable creating user-customized property grid tables...
  5. Advanced JIDE Property Grids JIDE property grids can use complex cell renderer and editor components and can signal property change events asynchronously to Matlab callbacks...
  6. Undocumented scatter plot behavior The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific point as it returns with 100 or less points....

Categories: Handle graphics, Hidden property, Medium risk of breaking in future versions, Undocumented feature

Tags: , , ,

Bookmark and SharePrint Print

9 Responses to Plot LineSmoothing property

  1. Tim says:

    There are more issues with LineSmoothing. I’ve seen some situations where the lines don’t get drawn at all if line smoothing is on. They should really fix this and turn it on by default.

  2. Justin says:

    I find that line smoothing has an odd side effect when plotting with datetick (using version 7.6.0.324 = R2008a). Try this:

    t = 730486:734869;
    plot(t,t, 'LineSmoothing','off')

    You should get a plot with tick labels around 7.3 and then an exponent label on the axes like “x10^5″. The following command changes the x-axis tick labels to dates:

    datetick x

    And everything works as expected. Now repeat the above but set LineSmoothing to ‘on’. I find now, when datetick is called, that the tick labels change to dates but the “x10^5″ label doesn’t disappear. Which is kind of confusing!?

    • mattdash says:

      Justin – I think the problem is just that line smoothing uses transparency, so it only works with the open gl renderer. I get the same bug you describe using open gl without turning on line smoothing.

      t = 730486:734869;
      plot(t,t, 'LineSmoothing','off')
      datetick x
      set(gcf,'renderer','opengl')
  3. Simon says:

    This appears to be ‘on’ by default on the Mac version, because all plots are antialiased (and look quite good, too).

    Is there a way to force this value to ‘on’ by default in the windows version? The standard set(gca,’DefaultLineSmoothing’,'on’) method does not work.

    • @Simon – LineSmoothing is not an axes property but a plot object (line/patch/etc.) property. Also, default properties should be set on the root (0) handle. Therefore, the correct syntax is:

      set(0,'DefaultLineLineSmoothing','on')
      set(0,'DefaultPatchLineSmoothing','on')
      etc.

      It’s a bit confusing at first, but makes perfect sense once you look at it.

  4. Richard says:

    I can also confirm that this seems to be an opengl issue. Setting the renderer to opengl causes the FaceColor of rectanges (plotted using rectangle) to disappear when plotted on top of an area plot, which has a filled background. I suspect there is a general issue here with the opengl renderer and the plotting order algorithm that needs fixed.

  5. Rene says:

    I found another issue with LineSmoothing.
    I have two axes in my figure. The first one contains multiple rectangles, all share the same ButtonDownFcn. In the other axes I use plot(…) with LineSmoothing turned On.
    Lets say I have four rectangles in the first axes.
    The effect is that, if I click into a non-rectangle area in the first axes, the ButtonDownFcn is activated and with each click, a different rectangle is passed as the first argument.
    After four clicks everything behaves as expected again.
    When I click on a rectangle, the same happens except that the number of clicks until I reach a consistent behavior again depends on the rectangle. E.g. the first rectangle needs 4 clicks, the second 3 clicks, the third 2 clicks and the last one only 1 click.
    When I disable LineSmoothing, everything works as expected.
    My OS is MS Windows 7 and the MATLAB Version is 7.11.0 (R2010b)

    • Rene says:

      P.S: my figure’s renderer is set to “painters”. When I change it to “opengl”, not equal but similar click-issues occur.

  6. John says:

    Confirming odd behavior that appear to stem from ‘linesmoothing’.
    This will show two curves – one regular and one smooth.
    You can graphically select either one (click) and delete, but “Undo Delete” will not bring the curve back.
    It shows the black squares where the curve ought to be, but the curve is not rerendered.

    Using ‘painters’ and ‘zbuffer’ solve this problem, but do not give you smooth curves.

    Matlab, can you please fix this?

    clear all
    x = linspace(0,1,10);
    y1 = sin(x);
    y2 = cos(x);
     
    figure
    hold on
    set(gcf, 'renderer','opengl')
    plot(x, y1, 'b')
    plot(x, y2, 'r', 'linesmoothing','on')

Leave a Reply

Your email address will not be published. Required fields are marked *

*

<pre lang="matlab">
a = magic(3);
sum(a)
</pre>