2

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?

Suever
  • 64,497
  • 14
  • 82
  • 101

1 Answers1

2

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.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • I was about to answer the same! I found this undocumented property by looking into `d = errorbar(...)` by calling `struct(d)`, which reveals more about the `ErrorBar` object. There I found many properties, the first being the `d.Bar`, which is a `LineStrip`, which in turn has a `LineStyle` property that can take values `'solid' | 'dashed' | 'dotted' | 'dashdot' | 'none'`. You could add this information to your answer to elucidate the origin and how others might investigate these situations themselves. – Erik Aug 02 '16 at 12:58
  • really thank you, I didn't know about this property and now I understand why, given that it is undocumented. Very good suggerstion, thanks. – Transagonistica Aug 02 '16 at 13:09
  • @Erik The better way to get information is via `meta.class`. Updated the answer to show how to do that. – Suever Aug 02 '16 at 13:15
  • @Transagonistica See the update which allows you to get more info and also to just match the error bar style to whatever you specify as the linestyle automatically. – Suever Aug 02 '16 at 13:19