Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust error bar width in Matlab

Tags:

plot

matlab

This is my problem:

My problem :-)

I have a MATLAB plot with errorbar (all work right), but the width of the bars is too wide. There is a way to set the width of the bar?

If you look this image very carefully, you can see several lines reds and blues with the size that I would like (e.g., w = 0.25).

Any help is appreciate.

like image 288
Giacomo Alessandroni Avatar asked May 30 '26 06:05

Giacomo Alessandroni


1 Answers

You need to access their XData property and modify them. Check here for an example by The Mathworks.

Concretely here is how to do it:

Generate an errorbar plot:

hf = figure;
X = 0:pi/10:pi;
Y = sin(X);
E = std(Y)*ones(size(X));

hErrBar = errorbar(X,Y,E);

Get the XData property as well as the left/right indices representing the horizontal lines of the error bars.

hb = get(hErrBar,'children');  
Xdata = get(hb(2),'Xdata');

temp = 4:3:length(Xdata);
temp(3:3:end) = [];

xleft = temp; xright = temp+1;

Modify the data as you wish and update the plot. For example, decrease the line length by 0.2 units

Xdata(xleft) = Xdata(xleft) + .1;
Xdata(xright) = Xdata(xright) - .1;

%// Update
set(hb(2),'Xdata',Xdata)

So for example,

Before:

enter image description here

And after:

enter image description here

like image 198
Benoit_11 Avatar answered May 31 '26 18:05

Benoit_11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!