Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Plot LineSmoothing property

March 17, 2010 34 Comments

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

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

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

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

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)

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. Plot line transparency and color gradient – Static and interpolated (gradient) colors and transparency can be set for plot lines in HG2. ...
  2. Plot markers transparency and color gradient – Matlab plot-line markers can be customized to have transparency and color gradients. ...
  3. 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. ...
  4. 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....
  5. Controlling plot data-tips – Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....
  6. Plot LimInclude properties – The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....
Handle graphics Hidden property Optical illusion Pure Matlab Undocumented property
Print Print
« Previous
Next »
34 Responses
  1. Tim March 17, 2010 at 17:57 Reply

    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 March 23, 2010 at 05:58 Reply

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

    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

    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 March 25, 2010 at 09:23 Reply

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

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

  3. Simon April 7, 2010 at 06:26 Reply

    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.

    • Yair Altman April 7, 2010 at 06:51 Reply

      @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.

      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 January 28, 2011 at 05:15 Reply

    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 June 21, 2011 at 02:17 Reply

    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 June 21, 2011 at 02:23 Reply

      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 September 22, 2011 at 15:14 Reply

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

    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 April 27, 2012 at 22:19 Reply

    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)

    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 June 7, 2012 at 08:48 Reply

    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 June 24, 2012 at 08:34 Reply

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

    • srynoname June 24, 2012 at 08:49 Reply

      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)

      • Yair Altman June 24, 2012 at 09:00

        @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 September 23, 2012 at 16:02 Reply

    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.

    • Yair Altman September 24, 2012 at 00:26 Reply

      @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. Waterloo graphics | Undocumented Matlab November 28, 2012 at 11:02 Reply

    […] One of Matlab’s limitations is the quality of its 2D graphics. Plots are more pixellated and less interactive than some might expect in a modern programming environment. […]

  12. Wangari January 9, 2013 at 03:00 Reply

    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.

    • Yair Altman January 9, 2013 at 03:22 Reply

      @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. Making Pretty Data Figures in Matlab | hippocampish April 18, 2013 at 21:09 Reply

    […] Without tweaking, Matlab line plots often look rough around the edges, but there’s a quick and easy fix: turning the LineSmoothing property ‘on’ […]

  14. Roman May 9, 2013 at 02:24 Reply

    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. HG2 update | Undocumented Matlab May 16, 2013 at 07:54 Reply

    […] Apparently, MathWorks solved the problems with the existing undocumented LineSmoothing property. Still, for some unknown reason, LineSmoothing remains a hidden/undocumented property. […]

  16. Lubos Smolik June 11, 2013 at 01:52 Reply

    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 October 4, 2013 at 06:04 Reply

    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.

    • Yair Altman October 4, 2013 at 06:07 Reply

      @Brad – try using the ScreenCapture utility.

      If you insist on using the OpenGL buffer, read this: http://stackoverflow.com/questions/8862467/using-opengl-in-matlab-to-get-depth-buffer

  18. Hossein Hadian November 26, 2013 at 03:00 Reply

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

  19. nikhil January 8, 2014 at 04:40 Reply

    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 January 8, 2014 at 05:43 Reply
      plot(1:5,2:6,'-','LineWidth',1,'LineSmoothing','on');
      hold on; box on;

      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 January 9, 2014 at 03:50

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

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

        opengl('software')

  20. Matlab: Smoothing Out Pixelated Lines | adilapapaya August 1, 2014 at 14:10 Reply

    […] Line in Matlab can look really pixelated. To get around that, you need to turn on line smoothing. […]

  21. Greg August 21, 2014 at 03:47 Reply

    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 September 28, 2014 at 07:03 Reply

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

      • Yair Altman September 28, 2014 at 08:56
        set(hPatch,'EdgeColor',get(gca,'color'), 'LineSmoothing','on')

        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.

  22. Naeem Ullah May 17, 2017 at 23:27 Reply

    Line smoothing is very helpful for me. Thank you

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top