Bar chart with double Y axis and error bars
Mostrar comentarios más antiguos
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')
Respuestas (1)
Star Strider
el 5 de Dic. de 2018
0 votos
We cannot run your code, so we cannot give a specific example with respect to it.
3 comentarios
KDRA
el 5 de Dic. de 2018
Star Strider
el 5 de Dic. de 2018
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
el 13 de Dic. de 2018
Categorías
Más información sobre Bar Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!