How to plot correctly errorbars on grouped bar plot?

조회 수: 3 (최근 30일)
Marianna Pizzo
Marianna Pizzo 2024년 5월 6일
댓글: Voss 2024년 5월 7일
Hello everyone,
I am using a livescript to present some data.. data is a 4x7 matrix, so nbars is 4, ngroups is 7
figure
labels = categorical({'Mental Demand' 'Physical Demand' 'Temporal Demand' 'Performance', 'Effort', 'Frustration', 'Overall'});
b=bar(labels',data');
title('RAW NASA TLX');
box off
hold on
% Calculate the number of groups and number of bars in each group
[ngroups,nbars] = size(data');
% Get the x coordinate of the bars
x = nan(nbars,ngroups);
for i = 1:nbars
x(i,:) = b(i).XEndPoints;
end
% Plot the errorbars
errorbar(x',data',error','k','linestyle','none');
hold off
legendText = cell(1, numel(A));
for i = 1:numel(A)
cameraType = A{i};
legendText{i} = cameraType;
end
legend(legendText);
The resulting plot in the livescript is wrong: all the errorbars are in the center of each of the respective group
But if I put the same code in the command window the plot is correct
Can you explain me what is going on?
I am sure that I am doing something wrong but I can not understand why (I took the errorbar part from some code online, it seems right to me but idk...)

채택된 답변

Voss
Voss 2024년 5월 6일
I am able to reproduce the problem; it looks like a bug in MATLAB, having to do with creating an errorbar in an axes that has a categorical x-axis, within a live script context.
A workaround is to use a numeric x-axis instead of categorical. Something like this (I've run this same code in a live script, and it produces the same result):
% random data, for demonstration:
data = 20+80*rand(4,7);
% random error, for demonstration:
err = 25*rand(4,7); % don't use "error" as a variable name, since it's a built-in function name
figure
labels = categorical({'Mental Demand' 'Physical Demand' 'Temporal Demand' 'Performance', 'Effort', 'Frustration', 'Overall'});
% number of groups:
ngroups = size(data,2);
% create the bars, using integers instead of categorical labels
b = bar(1:ngroups,data);
% set the x-ticks and x-tick labels:
xticks(1:ngroups)
xticklabels(labels)
title('RAW NASA TLX');
box off
hold on
% Get the x coordinate of the bars (a shorter way)
x = get(b,'XEndPoints');
x = vertcat(x{:});
% Plot the errorbars
errorbar(x,data,err,'k','linestyle','none');
hold off
Note that the groups there are in the order you specified in labels, which is not the case by default when using a categorical x-axis (as in your example, they're sorted alphabetically). If you want the groups to be in alphabetically-sorted order, you can do this:
figure
labels = categorical({'Mental Demand' 'Physical Demand' 'Temporal Demand' 'Performance', 'Effort', 'Frustration', 'Overall'});
% number of groups:
ngroups = size(data,2);
% sort the labels into alphabetical order:
[sorted_labels,sort_idx] = sort(lower(labels));
% create the bars, using integers instead of categorical labels
b = bar(1:ngroups,data(:,sort_idx));
% set the x-ticks and x-tick labels:
xticks(1:ngroups)
xticklabels(sorted_labels)
title('RAW NASA TLX');
box off
hold on
% Get the x coordinate of the bars (a shorter way)
x = get(b,'XEndPoints');
x = vertcat(x{:});
% Plot the errorbars
errorbar(x,data(:,sort_idx),err(:,sort_idx),'k','linestyle','none');
hold off
  댓글 수: 2
Marianna Pizzo
Marianna Pizzo 2024년 5월 7일
Thanks for the help :) Probelm solved
Voss
Voss 2024년 5월 7일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by