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. Getting default HG property values Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...
  3. Plot LimInclude properties The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....
  4. Controlling plot data-tips Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....
  5. Plot line transparency and color gradient Static and interpolated (gradient) colors and transparency can be set for plot lines in HG2. ...
  6. Draggable plot data-tips Matlab's standard plot data-tips can be customized to enable dragging, without being limitted to be adjacent to their data-point. ...

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

Tags: , , ,

Bookmark and SharePrint Print

33 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')
  7. Ali says:

    When I run following code, I see some white dots along the circle. I would be grateful if someone can help me with this problem. If you make m = 1, and change view to view(0,90), you will not see them. However, I need 3D view and I need to zoom out also. Many thanks in advance!

    t=0:2*pi/100:2*pi;x=cos(t);y=sin(t);
    plot(x,y,'linewidth',2,'linesmoothing','on');
    m=3;xlim([-m,m]);ylim([-m,m]);view(3)

    Ali

  8. Ruth says:

    Agree with Richard, John and others, seems to be a problem with linesmoothing in combination with the renderer.

    Using linesmoothing, in my case, both the x and y axis disappear, but only on the bottom and left side (top and right still there). The ticks and tick labels are still there on all sides though. xlabel disappears as well, but not ylabel. And the legend seems to be “cut off” on the bottom and left side.

    Tried same solution as John, same result. Also tried simple solutions, e.g. drawing black lines where the axes were supposed to be. Didn’t work, the lines were not displayed at all.

    Using Matlab version 7.9.0 (R2009b) on Windows 7. And hope there will be a solution to this soon!!!

  9. srynoname says:

    I have the same problem like Ruth, e.g. my x-axis is gone when using LineSmoothing. Any solution for this problem? Thanks!

    • srynoname says:

      Update: Problem occurs only in the figure window, as soon as I export the figure e.g. to PNG everything is ok (in the png file, in the figure window it is still missing the axis)

    • @Srynoname – it’s probably a renderer issue – switch the figure’s renderer property. In the future please use a real name and address – this is a serious website.

  10. Oliver says:

    Nice find. One thing I noticed while testing this today on Matlab 2012a, OS X 10.8.2, is that LineSmoothing works with plot() but not semilogx/y(). Also, if you use plot() and semilogx() in different subplots in the same figure, then LineSmoothing seems to stop working for the subplots containing plot() as well.

    • @Oliver – LineSmoothing only works if the figure Renderer is (or can be converted to) OpenGL, because it is apparently an OpenGL feature, as Matt Dash has noted above. When you use semilogx/y, the renderer needs to be ZBuffer because OpenGL does not support exponential axes.

  11. Pingback: Waterloo graphics | Undocumented Matlab

  12. Wangari says:

    I have a problem I want to loop over a plot such that I do not keep writing a plot when I use a different initial condition. My initial conditions are in a list. Please help me if you have any suggestion.

    • @Wangari – you can simply update the existing plot’s XData/YData/ZData properties when you change the initial conditions.

      If you don’t want to even do this, then you can set the plot’s XDataSource/YDataSource/ZDataSource property to the name of your workspace variable that will contain the modified data, and then the plot will automatically be updated.

  13. Pingback: Making Pretty Data Figures in Matlab | hippocampish

  14. Roman 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)

  15. Pingback: HG2 update | Undocumented Matlab

  16. I found another problem caused by LineSmoothing. If you need to export figure with smooth lines to EPS, you don’t obtain vector image but bitmap (in EPS). Yep, there is usually no need to smoothing things when you are exporting figures to vector images but sometimes you need both bitmap and vector which look pretty.

    I guess the issue is caused directly by the renderer. Luckily, if figures are exported programmaticaly LineSmoothing can be switched on or off in accordance with your requirements.

  17. Brad says:

    I’ve not seen a way to export a figure and capture the linesmoothing, except for using windows PrintScreen and cropping which looks uh, ‘croppy.’ Since this is a feature dependent upon OpenGL, do you know a way to export straight from OpenGL? GhostScript maybe? Incidentally, I just checked this on r2013B and it remains an issue.

  18. Hossein Hadian says:

    Such a great option and I didn’t know about! Thank you very much :-)

  19. nikhil says:

    Hello Yair Altman,

    When I run the program in R2010b and turn the lineSmoothing property to ‘on’ the border for the axis is disappearing. I tried to switch the ‘box’ property to ON and still doesn’t work like

    But every thing looks fine when it’s run in R2012b. Would you please suggest some solution?

    • nikhil says:
      plot(1:5,2:6,'-','LineWidth',1,'LineSmoothing','on');
      hold on; box on;

      Sorry I forgot to copy the code in previous comment.

    • Yair Altman says:

      Line-smoothing is an OpenGL feature and so when you use it the figure changes its Renderer to ‘OpenGL’. In this renderer some of the axes borderlines disappear. This problem occurs in all Matlab releases, not just 10b, and it is not related to LineSmoothing but rather to a bug when Matlab uses OpenGL hardware. For example, this will also display the bug:

      plot(1:5,2:6); set(gcf,'Renderer','OpenGL')

      The workaround is to use OpenGL software-simulation mode – this displays the borderlines correctly (note: software mode is slower than hardware mode):

      opengl('software')
  20. Pingback: Matlab: Smoothing Out Pixelated Lines | adilapapaya

  21. Greg says:

    Thanks, this works well for my patch plots. Do you know if there is a similar command to smooth the edges of the patch itself such that if I turn off the lines of my patch the block shapes will have smoothed edges?
    I prefer to use the patches with the lines set to off otherwise the objects appear larger than they really are.

    • Will Adler says:

      I also would appreciate an answer to Greg’s question!

    • Yair Altman says:
      set(hPatch,'EdgeColor',get(gca,'color'), 'LineSmoothing','on')

      is as close as you can get with HG1.

      In HG2 it should look much better since anti-aliasing is turned on by default and is supported across the board.

Leave a Reply

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

*

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