필터 지우기
필터 지우기

how to add errorbars

조회 수: 14 (최근 30일)
aviran
aviran 2014년 6월 19일
댓글: Star Strider 2022년 6월 6일
i want to add errorbars to this graph (the error bar are "std_150,std_100 and std_control" in the code).
thanks
the code:
mw_150=[14.5,19,8.5];
mw_100=[10.42,17.4,17.7];
control=[7.15,41.9,44.3];
std_150=[1.16,1.33,0.9];
std_100=[0.83,1.01,1.09];
std_control=[0.71,1.72,1.77];
bar([1 2 3],[mw_150' mw_100' control'])
legend('150mJ/cm2','100mJ/cm2','control');
set(gca, 'FontSize',12,'XTick',[1 2 3 ],'XTickLabel',{'1.5<A.R<2.5','2.5<A.R<3.5','A.R>3.5' });
ylabel('Relative precentage')
  댓글 수: 2
dpb
dpb 2014년 6월 19일
Not sure exactly how to envision error "bars" on a bar chart...can you describe what you think it should look like?
Only thoughts come to mind are either use a stacked bar but doesn't really seem like it would work well or use errorbar with the adjust x- coordinates for the bars and no line on the plotted line. I've not tried to see if can convince errorbar to draw the bars but not the trend line directly or whether it would take finding/setting appropriate line handle linestyle property.
aviran
aviran 2014년 6월 19일
it should look like in the attached picture

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

채택된 답변

Star Strider
Star Strider 2014년 6월 19일
편집: Star Strider 2014년 6월 19일
This File Exchange contribution is highly regarded: Bar Chart with Error Bars.
EDIT —
If you’d rather have it look like your attached figure, this code will work:
mw_150=[14.5,19,8.5];
mw_100=[10.42,17.4,17.7];
control=[7.15,41.9,44.3];
std_150=[1.16,1.33,0.9];
std_100=[0.83,1.01,1.09];
std_control=[0.71,1.72,1.77];
hb = bar([1 2 3],[mw_150' mw_100' control'])
legend('150mJ/cm2','100mJ/cm2','control');
set(gca, 'FontSize',12,'XTick',[1 2 3 ],'XTickLabel',{'1.5<A.R<2.5','2.5<A.R<3.5','A.R>3.5' });
ylabel('Relative precentage')
errbar = [std_150; std_100; std_control]; % CREATE ‘errbar’ MATRIX
yd = [mw_150' mw_100' control']';
hold on
for k1 = 1:3
errorbar([1:3]+.22*(k1-2), yd(k1,:), errbar(k1,:), '.k', 'LineWidth',2)
end
hold off
  댓글 수: 11
Yeasir Mohammad Akib
Yeasir Mohammad Akib 2022년 6월 6일
Hello State Strider,
Can you please expalin the meaning of this line:
for k1 = 1:size(mw_data,1)
errorbar(([1:4]-mv)*sclf+k1 ,mw_data(k1,:), errbar(k1,:), '.k', 'LineWidth',2)
end
The .k portion
Star Strider
Star Strider 2022년 6월 6일
@Yeasir Mohammad Akib — It plots black dots, I believe to avoid connecting the error bars with lines. (This is nearly 8 year-old code. I do not remember what I was thinking back then when I wrote it.)

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

추가 답변 (1개)

dpb
dpb 2014년 6월 19일
편집: dpb 2014년 6월 19일
...scales the matrix to put the errorbars in the middle of the plotted bars, and I have no idea how MATLAB determines where it puts those bars. The handle graphics don’t say, because all the 'XData' values are the same for all variables. I cannot find anything in the documentation that alludes to where they are stored or how they are calculated...
Got's to go handle-diving Star--they're not identified per se, but you can compute the location by retrieving the XData property from the barseries object patch...
For the sample above for the first handle, one can find
>> xtik=get(get(hb(1),'children'),'xdata')
xtik =
0.6545 1.6545 2.6545
0.6545 1.6545 2.6545
0.8000 1.8000 2.8000
0.8000 1.8000 2.8000
>> mean(xtik(1:2:end,:))
ans =
0.7273 1.7273 2.7273
>>
By comparison, your heuristic values were
>> ([1]-mv)*sclf+[1:3]
ans =
0.7300 1.7300 2.7300
>>
Not bad... :)
  댓글 수: 2
Star Strider
Star Strider 2014년 6월 19일
Thank you! I went handle-diving, but apparently not deep enough.
dpb
dpb 2014년 6월 19일
No problem...came up with this w/ a poster's help when labeling a bar series w/ values. I'd always used a heuristic based on a the 'Width' parameter before but it wasn't completely reliable, either. This returns the real deal of where actually draws the patch object so is dead on...

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

카테고리

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