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 Center および File ExchangeErrorbars についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by