# Why is errorbar whisker length dependent on X?

4 views (last 30 days)
David K on 22 May 2015
Commented: David Karnick on 27 May 2015
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!

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')
David K on 27 May 2015
This is probably the best way to do it, thanks.

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 CommentsShowHide 1 older comment
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.