Error bars on categorical bar plot

62 visualizaciones (últimos 30 días)
NA
NA el 30 de Mayo de 2020
Comentada: the cyclist el 30 de Mayo de 2020
Hi,
I'm trying to plot error bars on my categorical plot, but the second error bar doesn't align with the second bar. Can someone please help me with this.
The version of MATLAB I'm using is R2019b.
My code is as follows:
%Define the colors' RGB values in a matrix
col(1,:) = [1 0 0]; % -- red
col(2,:) = [0, 0.4470, 0.7410]; % -- blue
x = categorical({'Control', 'Folic Acid'}); %xaxis
x = reordercats(x,{'Control', 'Folic Acid'}); %specified order
y = [1.8493, 1.8]; %yaxis
SEM=[0.0168, 0.0170];
figure; bar(x(1), y(1), 0.5, 'FaceColor',col(1,:)); hold on;
errorbar(y(1),SEM(1),'k','linestyle','none'); hold on;
bar(x(2), y(2), 0.5, 'FaceColor',col(2,:)); hold on;
errorbar(y(2),SEM(2),'b','linestyle','none'); hold off;
title('Brain Weight')
ylabel('Weight (g)')

Respuesta aceptada

the cyclist
the cyclist el 30 de Mayo de 2020
Editada: the cyclist el 30 de Mayo de 2020
Call errorbar with this syntax instead:
errorbar(x(1),y(1),SEM(1),'k','linestyle','none');
errorbar(x(2),y(2),SEM(2),'b','linestyle','none');
FYI, your code can be simplified to the following:
%Define the colors' RGB values in a matrix
col(1,:) = [1 0 0]; % -- red
col(2,:) = [0, 0.4470, 0.7410]; % -- blue
x = categorical({'Control', 'Folic Acid'}); %xaxis
x = reordercats(x,{'Control', 'Folic Acid'}); %specified order
y = [1.8493, 1.8]; %yaxis
SEM=[0.0168, 0.0170];
figure
hold on
b = bar(x, y, 0.5, 'FaceColor',col(2,:));
b.FaceColor = 'flat';
b.CData= col;
errorbar(x,y,SEM,'k','linestyle','none');
title('Brain Weight')
ylabel('Weight (g)')
hold off
You only need one call each for bar and errorbar, and the multiple calls to hold on are extraneous.
  2 comentarios
NA
NA el 30 de Mayo de 2020
Thank you very much. I'm quite new to MATLAB, that's why my code isn't as good as it could be. But I really appreciate your feedback!! Many thanks.
the cyclist
the cyclist el 30 de Mayo de 2020
Happy to help. You took the time to create a clear, well formatted forum question, so I think you are well on the way to learning more MATLAB!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by