matlabgraphicsmatlab-figurematlab-hg2

MATLAB Errorbars linestyle


When using MATLAB, does anyone know a way for drawing errorbars with the same style of the data line?

For example, when using:

d = errorbar(x,y,y_error,'Linestyle', ':');

MATLAB returns the data lines in dotted style, while the bars in each of the points are solid lines. How do I make the bars also be a dotted line?


Solution

  • You can use the undocumented Bar property of the ErrorBar object to set the line style:

    d = errorbar(1:3, 1:3, 1:3, 'LineStyle', ':');
    
    % Make the vertical bars dotted as well
    d.Bar.LineStyle = 'dotted';
    
    % Valid values include: 'solid' | 'dashed' |  'dotted' | 'dashdot' | 'none'
    

    enter image description here

    Or if you just want it to be the same as the LineStyle you specified you could also use the undocumented Line property:

    d.Bar.LineStyle = d.Line.LineStyle
    

    For future reference, you can get a list of all properties and methods for a graphics object (undocumented or not) by obtaining the meta.class for the object:

    cls = meta.class.fromName(class(d));
    
    % List of all properties
    cls.PropertyList
    
    % List of all methods
    cls.MethodList
    

    You can often find and modify the various pieces of a complex plot object using undocumented properties found in this way.