Why is errorbar whisker length dependent on X?

조회 수: 4 (최근 30일)
David K
David K 2015년 5월 22일
댓글: David Karnick 2015년 5월 27일
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
Chad Greene 2015년 5월 26일
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')
  댓글 수: 1
David K
David K 2015년 5월 27일
This is probably the best way to do it, thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chad Greene
Chad Greene 2015년 5월 22일
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
Chad Greene
Chad Greene 2015년 5월 22일
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 2015년 5월 26일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by