Why is errorbar whisker length dependent on X?

2 views (last 30 days)
I am running a simulation in Matlab 2015 which creates data each round, so I am plotting a number of single errorbar series individually. Here's a sample code and the output plot:
figure
xlim([0 30])
hold on
errorbar(1,2.0,1.9,16.5,'x')
errorbar(15,2.0,1.9,16.5,'x')
errorbar(25,2.0,1.9,16.5,'x')
errorbar(10,2.0,1.9,16.5,'x')
errorbar(30,2.0,1.9,16.5,'x')
errorbar(5,2.0,1.9,16.5,'x')
errorbar(20,2.0,1.9,16.5,'x')
The weirdness is that the whisker length appears to be dependent on my X value. As you can see it increases from left to right, even though I did not plot them from left to right. The single-side width of each whisker is 0.01*x.
What on earth is happening, and how can I fix it? Ideally I would just manually set the width of each whisker, but I can't find a way to do that. There was a function posted on the MFX a while back, but that was made for R2007b and no longer works. The errorbar series properties do not give any indication that the whiskers can even be affected at all. Any help here would be greatly appreciated!

Accepted Answer

Chad Greene
Chad Greene on 26 May 2015
You could make the bars manually like this:
% define data:
x = [1 15 25 10 30 5 20];
shw = 0.3; % half-width of stem in x units
y = 2+zeros(size(x));
le = 0.9+zeros(size(x)); % lower error
ue = 16.5+zeros(size(x)); % upper error
figure
xlim([0 31])
hold on
% definte bar colors:
colors = jet(length(x));
for k = 1:length(x)
% x-shaped data marker:
plot(x(k),y(k),'x','color',colors(k,:),'markersize',12)
% vertical bar:
plot([x(k) x(k)],y(k)+[-le(k) ue(k)],'-',...
'color',colors(k,:),'linewidth',1)
% horizontal stems:
plot(x(k)+shw*[-1 -1;1 1],...
y(k)+[-le(k) ue(k);-le(k) ue(k)],...
'-','color',colors(k,:),'linewidth',1)
end
set(gcf,'color',.2*[1 1 1])
set(gca,'color','none')

More Answers (1)

Chad Greene
Chad Greene on 22 May 2015
I get the same behavior in 2012b. The culprit may be on line 195 of the errorbar function, which says:
tee = (max(x(:))-min(x(:)))/100; % make tee .02 x-distance for error bars
You can fix it by calling errorbar only once:
figure
xlim([0 30])
hold on
x = [1 15 25 10 30 50 20]';
errorbar(x,2*ones(size(x)),1.9*ones(size(x)),16.5*ones(size(x)),'x')
  2 Comments
Chad Greene
Chad Greene on 22 May 2015
I personally dislike the endbits, so I wrote a function called errbar. Syntax would be:
figure
xlim([0 30])
hold on
x = [1 15 25 10 30 50 20];
y = 2*ones(size(x));
plot(x,y,'x')
errbar(x,y,1.9,16.5)
David K
David K on 26 May 2015
Thanks for the suggestions. Unfortunately I do need to plot them individually, as I would like to apply different colors and weights to different bars. I looked at the errbar function, which is nice, but I do want to keep the end caps and center marks.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!