how can I place my error bar in separate bar center?
6 次查看(过去 30 天)
显示 更早的评论
load SIwb ( % load the workbook of data)
whos LTST PhA_SD BSIR SIR (% find variable)
G = findgroups(LTST, PhA_SD);(% Grouping the variables)
BSIRm=splitapply(@nanmean,BSIR,G);(% Mean of each group of BSIR)
SIRm=splitapply(@nanmean,SIR,G);(% Mean of each group of SIR)
SIRc=[BSIRm,SIRm];(% Combine the SIR mean of Baseline and after SD)
(%Calculate the standard error,SEMM function is uploaded)
BLSIRe=splitapply(@SEMM,BSIR,G);
SIRe=splitapply(@SEMM,SIR,G);
(%combine the std error of BLSIR and SIR)
SIRec=[BLSIRe, SIRe];
(%split the std error into LT and ST)
LTSIRe=SIRec(1:4,:);
STSIRe=SIRec(5:8,:);
(%split the LT and ST)
LTSIR=SIRc(1:4,:);
STSIR=SIRc(5:8,:);
(%Plot the LT graph)
subplot(2,2,1)
(%Plot the data of SI for Long term)
H=bar(LTSIR);
hold on
errorbar(LTSIR,LTSIRe,'.')
%--------------------
legend('Baseline-SIR','SIR','Location','northwest');
grid on
P=gca();
set(P,'XTICKLABEL',{'CH-PhA-','CH-PhA+','SD-PhA-','SD-PhA+'});
ylabel(P,'SIR(%)');
title('LT stress and PhA impact on SIR','FontSize',10)
(%Set up color of each group of bar)
set(H(1),'FaceColor','k');
set(H(2),'FaceColor','r');
The work book data is in attachment.
0 个评论
采纳的回答
dpb
2018-1-29
编辑:dpb
2018-1-30
See the "trick" to locate the midpoint of stacked bars from the hidden property at How-do-i-label-each-bar-in-bar-group-with-a-string-on-top
I don't know if it's yet been made visible or not; add to the complaints to TMW about how poorly-designed the BAR function user interface is in general; I've railed for 20+ years.
ADDENDUM
Stab at adapting to your variables above...
H=bar(LTSIR);
hold on
X=[]; % placeholder for X position from bar
for i=1:length(H) % iterate over number of bar objects
X=[X H(i).XData+hB(i).XOffset]; % and get the center bar group positions
end
hE=errorbar(X,[LTSIR LTSIRe],'.')
You'll have to check on orientation/size of X vector vis a vis the error data arrays but the idea is to retrieve the X locations of the midpoints of the bar and then plot the error bars versus it instead of the nominal bar position.
Again, this kind of thing should be available as properties/methods of the bar object already.
ADDENDUM 2
OK, I had a few minutes...
AB=randi([110 160],4,2); % make some sample data
hB=bar(AB); % make default bar plot
ylim([0 200]) % scale same for rough appearance similar
e=std(AB); % get a sample error value
X=[]; % collect the X values with offset for each
for i=1:length(hB)
X=[X;hB(i).XData+hB(i).XOffset];
end
>> X=X.' % rearrange X by column and show what this is...
X =
0.8571 1.1429
1.8571 2.1429
2.8571 3.1429
3.8571 4.1429
>> hold on % so can add to bar plot
>> hEB=errorbar(X,AB,repmat(e,4,1),'.'); % add error bars
>>
The above gives the attached figure...
You'll have to clean up to match your colors, etc., etc., etc., but shows how to get the correct X positions to plot on each bar in the multiple-bar style.
ADDENDUM 2
Reflection made me realize don't need the loop to retrieve the X coordinates--
X=cell2mat(get(hB,'XData')).' + [hB.XOffset];
will do the same thing using the fact that get will retrieve the same property across multiple handles and return as cell array and knowing that the .XOffset property is a constant for each bar series then recent releases will do the addition using implicit singleton expansion. At the time I wrote the Answer linked to, the latter feature was not yet implemented in the release available to me at the time.
9 个评论
dpb
2018-1-30
Why regress to the specific computation that presumes TMW doesn't change anything inside bar instead of using the .XData and .XOffset properties from the actual object?
As the example shows, this works; if you got something unexpected you made an error; debug/fix it instead would be my recommendation.
更多回答(2 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!