Bar chart with double Y axis and error bars

I have bar chart with two Y axes and I want to add error bars to the graph. This is my code (works perfectly w/o error bars but I am not sure how to add them):
g = figure
subplot(1,2,1)
a = [HINT_av zeros(1,4)];
b = [zeros(1,4) DANTALE_av];
[AX,H1,H2] = plotyy([1:8],a, [1:8],b, 'bar', 'bar');
set(H1,'FaceColor','r')
set(H2,'FaceColor','b')
xt = get(gca, 'Xtick');
set(gca, 'Xtick', xt, 'XTickLabel', {'F1', 'F4', 'F5', 'F7', 'F1', 'F4', 'F5', 'F7'})
legend('HINT','DANTALE')
grid minor
title('Average speech test results')

답변 (1개)

Star Strider
Star Strider 2018년 12월 5일

0 개 추천

See: Bar Plot With Error Bar (link) for an example.
We cannot run your code, so we cannot give a specific example with respect to it.

댓글 수: 3

KDRA
KDRA 2018년 12월 5일
These are my variables:
HINT_av = [0.325 -0.29 -0.35 -0.35];
DANTALE_av = [73.93 73.34 74.4 73.8]
And these are standard deviations:
HINT_sd = [1.35262091264823 1.01044544632553 1.37133349538161 1.22497165500086];
DANTALE_sd = [6.73119603042431 10.1497892698431 5.68174464598878 6.02716074670874]
that should be enough?
I can get this to work for ‘HINT’ only. The reason is that MATLAB deletes the second Axes object (‘AX(2)’) and the associated Bar object before or in the for loop, and I can’t figure out how to prevent that.
Your posted code with my additions:
HINT_av = [0.325 -0.29 -0.35 -0.35];
DANTALE_av = [73.93 73.34 74.4 73.8];
HINT_sd = [1.35262091264823 1.01044544632553 1.37133349538161 1.22497165500086];
DANTALE_sd = [6.73119603042431 10.1497892698431 5.68174464598878 6.02716074670874];
g = figure;
% subplot(1,2,1)
a = [HINT_av zeros(1,4)];
b = [zeros(1,4) DANTALE_av];
[AX,H1,H2] = plotyy([1:8],a, [1:8],b, 'bar', 'bar');
hold all
set(H1,'FaceColor','r')
set(H2,'FaceColor','b')
xt = get(gca, 'Xtick');
set(gca, 'Xtick', xt, 'XTickLabel', {'F1', 'F4', 'F5', 'F7', 'F1', 'F4', 'F5', 'F7'})
legend('HINT','DANTALE', 'Location','NortheastOutside')
grid minor
title('Average speech test results')
AX(1).YLim = [-3 3];
AX(2).YLim = [0 85];
hBar = [H1; H2]
sd_mtx = [HINT_sd; DANTALE_sd];
for k1 = 1:size(AX,2)
ctr(k1,:) = bsxfun(@plus, hBar(k1).XData, hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
YLI = ydt(k1,:) ~= 0; % Logical Mask To Create Equal Vectors
xv = ctr(k1,YLI);
yv = ydt(k1,YLI);
err = sd_mtx(k1,:);
errorbar(AX(k1), xv, yv, err, '.r') % Plot Error Bars
end
hold off
Anyway, I gave it my best effort, and half of it works. I am going to report ehe axes deletion as a bug. It may not exist in the newer yyaxis (link) function (I didn’t try it), and since you didn’t use it, you may not have it, so I didn’t change your code.
KDRA
KDRA 2018년 12월 13일
Hi there!
thanks for your effort:)
I actually did try the yyaxis at first but I didn't know how to plot bar charts with this (I managed to do it for line plots).
So if you may, it would be great if you could give me some tips to make it working! Thanks in advance!

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

카테고리

질문:

2018년 12월 5일

댓글:

2018년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by