How to produce a grouped errorbars?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi all,
I am trying to produce a grouped errorbars with the attached data.
"Mean' is 5x3 matrix with means and 'std' is the same with standard deviations of those means.
The ouput figure should look like more or less like this, with the exception I should have 5 error bars per group (not 3 like in the image), with diffrent colours for means.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/943574/image.bmp)
More precisle what I mean, using an example of just one group
figure
subplot(3,3,1)
Group 'Sag'
errorbar(mean(1,1), std(1,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','red','MarkerFaceColor','red', 'LineWidth', 1);
errorbar(mean(2,1), std(2,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','green','MarkerFaceColor','green', 'LineWidth', 1);
errorbar(mean(3,1), std(3,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','black','MarkerFaceColor','black', 'LineWidth', 1);
errorbar(mean(4,1), std(4,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','yellow','MarkerFaceColor','yellow', 'LineWidth', 1);
errorbar(mean(5,1), std(5,1),'-s','MarkerSize',10,...
'MarkerEdgeColor','blue','MarkerFaceColor','blue', 'LineWidth', 1);
Group 'Frnt'
errorbar(mean(1,2), std(1,2),'-s','MarkerSize',10,...
'MarkerEdgeColor','red','MarkerFaceColor','red', 'LineWidth', 1);
etc
Can you help please?
0 commentaires
Réponse acceptée
Voss
le 28 Mar 2022
Modifié(e) : Voss
le 28 Mar 2022
Maybe something like this:
load('mean.mat')
load('std.mat')
mean = mean.';
figure();
ax = gca();
hold on
% to be adjusted for different numbers of groups or bars/group:
group_names = {'Sag','Frnt','Tran'};
bar_colors = 'rgkyb';
[n_groups,n_bars] = size(std);
x_min = 0.5;
x_max = n_groups*n_bars+0.5;
y_min = min(mean(:)-std(:));
y_max = max(mean(:)+std(:));
dy = y_max-y_min;
y_min = y_min-0.05*dy;
y_max = y_max+0.05*dy;
% grey shading on top:
p = patch( ...
'Parent',ax, ...
'XData',[x_min x_max x_max x_min], ...
'YData',[0.75 0.75 1 1]*(y_max-y_min)+y_min, ... % not sure exactly where the grey box goes this time
'FaceColor',[0.8 0.8 0.8], ...
'EdgeColor','none');
% line separating axes into 3 parts:
gl = line( ...
'Parent',ax, ...
'XData',[1 1 NaN 2 2]/3*(x_max-x_min)+x_min, ...
'YData',[y_min y_max NaN y_min y_max]);
% error bars:
for ii = 1:n_bars
h = errorbar((0:n_groups-1)*n_bars+ii,mean(:,ii),std(:,ii),'-sk', ...
'MarkerSize',10,...
'MarkerEdgeColor',bar_colors(ii), ...
'MarkerFaceColor',bar_colors(ii), ...
'LineWidth',1, ...
'LineStyle','none');
end
set(ax, ...
'Box','on', ...
'Layer','top', ...
'XLim',[x_min x_max], ...
'YLim',[y_min y_max], ...
'XTick',(n_bars+1)/2+n_bars*(0:n_groups-1), ...
'XTickLabel',group_names);
3 commentaires
Voss
le 28 Mar 2022
Keep each bar as it is created by making h into a vector, then use h in legend() with whatever names you want to call them, and you can use 'Location','best'
load('mean.mat')
load('std.mat')
mean = mean.';
figure();
ax = gca();
hold on
% to be adjusted for different numbers of groups or bars/group:
group_names = {'Sag','Frnt','Tran'};
bar_colors = 'rgkyb';
[n_groups,n_bars] = size(std);
x_min = 0.5;
x_max = n_groups*n_bars+0.5;
y_min = min(mean(:)-std(:));
y_max = max(mean(:)+std(:));
dy = y_max-y_min;
y_min = y_min-0.05*dy;
y_max = y_max+0.05*dy;
% grey shading on top:
p = patch( ...
'Parent',ax, ...
'XData',[x_min x_max x_max x_min], ...
'YData',[0.75 0.75 1 1]*(y_max-y_min)+y_min, ... % not sure exactly where the grey box goes this time
'FaceColor',[0.8 0.8 0.8], ...
'EdgeColor','none');
% line separating axes into 3 parts:
gl = line( ...
'Parent',ax, ...
'XData',[1 1 NaN 2 2]/3*(x_max-x_min)+x_min, ...
'YData',[y_min y_max NaN y_min y_max]);
% error bars:
for ii = 1:n_bars
h(ii) = errorbar((0:n_groups-1)*n_bars+ii,mean(:,ii),std(:,ii),'-sk', ...
'MarkerSize',10,...
'MarkerEdgeColor',bar_colors(ii), ...
'MarkerFaceColor',bar_colors(ii), ...
'LineWidth',1, ...
'LineStyle','none');
end
set(ax, ...
'Box','on', ...
'Layer','top', ...
'XLim',[x_min x_max], ...
'YLim',[y_min y_max], ...
'XTick',(n_bars+1)/2+n_bars*(0:n_groups-1), ...
'XTickLabel',group_names);
legend(h,sprintfc('Bar %d',1:n_bars),'Location','best');
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!