Error Bars on horizontal bar chart

조회 수: 50 (최근 30일)
Nshine
Nshine 2019년 5월 6일
댓글: Adam Danz 2022년 4월 22일
Hi.
I am trying to produce a horizontal bar chart with error bard, but the error bars minimum value seem completely wrong. Also, the positions of the vertical 'ends' of each error bar appear to be in the wrong position. What am I doing wrong?
Here is the code that produced this chart below:
% data
min = [1e-4 1e-3 1e-2 1e-1 1e1];
mean = [1e-3 1e-2 1e-1 1e1 1e2];
max = [1e-2 1e-1 1e1 1e2 1e3];
cats = categorical({'a','b','c','d','e'});
% plot horizontal bars
figure; hold on;
p = barh(cats,mean,...
'FaceColor',[0.75,0.75,0.75],...
'EdgeColor','k',...
'LineWidth',1.5,...
'BaseValue',1);
baseline = p.BaseLine;
% change xscale to 'log'
ax = gca;
ax.XScale = 'log';
baseline.LineWidth = 1.5;
% add y tick labels
ax.YTickLabel = {'cat a','cat b','cat c','cat d','cat e'};
% plot error bars
er = errorbar(mean,cats,min,max,'.','horizontal');
er.LineWidth = 1.5;
er.Color = 'k';
er.MarkerSize = 1;
BarChart.PNG
  댓글 수: 3
Glaucia Fragoso
Glaucia Fragoso 2022년 4월 22일
I don't know how you did this... when I try I get an error "X-data must be the same size as Y-data"
Adam Danz
Adam Danz 2022년 4월 22일
The error message tells you what's wrong. Your x-data must be the same size as your y-data.
x = 1:5;
y = 1:6;
e = 1:6;
errorbar(x,y,e)
Error using errorbar
X-data must be the same size as Y-data.

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

채택된 답변

dpb
dpb 2019년 5월 6일
편집: dpb 2019년 5월 6일
"errorbar(x,y,neg,pos) draws a vertical error bar at each data point, where neg determines the length below the data point and posdetermines the length above the data point, respectively."
Looks like it did exactly what you askied it to do...it added the min,max values to the mean value and drew the errorbars at those computed limits. It appears instead you intended
er = errorbar(mean,cats,mean-min,mean+max,'.','horizontal');
BTW, do NOT use mean, max, min as variable names; they are the builtin functions of the same meaning; aliasing them will undoubtedly wreak havoc later on when you expect them to function normally. Use some identifier to differentiate like xmean, xmax, xmin or the like instead.
  댓글 수: 2
Adam Danz
Adam Danz 2019년 5월 6일
+1 for warning about variable names.
Nshine
Nshine 2019년 5월 7일
thanks dpb!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by