MATLAB Answers

Grouped bar graph with individual datapoints

96 views (last 30 days)
Varghese
Varghese on 29 Jul 2021
Answered: Dave B on 30 Jul 2021
I am trying to plot a grouped bar graph with error bars and individual datapoints. I was able to do the bargraph with errorbars. How can I add individual data points to each bar?
figure
model_series = [mean(Dat_100_200_E_M,1); mean(Dat_100_200_M_E,1)];
model_error = [std(Dat_100_200_E_M)/sqrt(length(Dat_100_200_E_M)); std(Dat_100_200_M_E)/sqrt(length(Dat_100_200_M_E))];
b=bar(model_series, 'grouped','FaceColor','flat');
legend ('boxoff');
b(1).CData = [[1 0 0]; [1 0 0]];
b(2).CData = [[1 1 0]; [1 1 0]];
ylim([0 6]);
hold on
nbars = size(model_series, 2);
x = [];
for i = 1:nbars
x = [x ; b(i).XEndPoints];
end
errorbar(x',model_series,model_error,'k','LineWidth', 1.5,'linestyle','none','HandleVisibility','off');
hold off
box off
ax = gca;
set(gca,'xticklabel',{'Left', 'Right'});
ylabel('Amplitude','FontSize', 12)
  2 Comments
Varghese
Varghese on 30 Jul 2021
Thank you for your response. The target figure looks something like this.
The 'Dat_100_200_E_M' is 18x2 array. I want to plot the indivudual data points from this array on the 'Left' category and 'Dat_100_200_M_E' on the 'Right' category.

Sign in to comment.

Accepted Answer

Dave B
Dave B on 30 Jul 2021
I think you have most of the info you need to plot the points, as you already found the XEndPoints property which really simplifies things. Using repmat can be helpful for repeating those values to match the sizes of the y data. Here's some example code that uses scatter to make the markers...I think it looks pretty similar to your example - other than my data are made up!
clf;
rng(pi) % just to reproduce the random data I used
x=randi(2,10,1);
y=rand(10,2)*6;
h=bar([mean(y(x==1,:));mean(y(x==2,:))]');
hold on
h(1).FaceColor='r';
h(2).FaceColor='y';
errorbar(h(1).XEndPoints,mean(y(x==1,:)),std(y(x==1,:)),'LineStyle','none','Color','k','LineWidth',2)
errorbar(h(2).XEndPoints,mean(y(x==2,:)),std(y(x==2,:)),'LineStyle','none','Color','k','LineWidth',2)
% simple version
scatter(repmat(h(1).XEndPoints(1), sum(x==1), 1),y(x==1,1),60,'MarkerFaceColor','r','MarkerEdgeColor','k','LineWidth',1)
scatter(repmat(h(1).XEndPoints(2), sum(x==1), 1),y(x==1,2),60,'MarkerFaceColor','r','MarkerEdgeColor','k','LineWidth',1)
scatter(repmat(h(2).XEndPoints(1), sum(x==2), 1),y(x==2,1),60,'MarkerFaceColor','y','MarkerEdgeColor','k','LineWidth',1)
scatter(repmat(h(2).XEndPoints(2), sum(x==2), 1),y(x==2,2),60,'MarkerFaceColor','y','MarkerEdgeColor','k','LineWidth',1)
% bonus version, it looks like the markers in your example have a tiny bit of
% jitter. Starting in R2020b you can do this easily with scatter:
%scatter(repmat(h(1).XEndPoints(1), sum(x==1), 1),y(x==1,1),60,'MarkerFaceColor','r','MarkerEdgeColor','k','LineWidth',1,'XJitter','randn','XJitterWidth',.05)
%scatter(repmat(h(1).XEndPoints(2), sum(x==1), 1),y(x==1,2),60,'MarkerFaceColor','r','MarkerEdgeColor','k','LineWidth',1,'XJitter','randn','XJitterWidth',.05)
%scatter(repmat(h(2).XEndPoints(1), sum(x==2), 1),y(x==2,1),60,'MarkerFaceColor','y','MarkerEdgeColor','k','LineWidth',1,'XJitter','randn','XJitterWidth',.05)
%scatter(repmat(h(2).XEndPoints(2), sum(x==2), 1),y(x==2,2),60,'MarkerFaceColor','y','MarkerEdgeColor','k','LineWidth',1,'XJitter','randn','XJitterWidth',.05)
xticklabels(["Left" "Right"])
ylabel("Amplitude")
(Below is the version with Jitter, because I like it better!)

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by