Grouped bar graph with individual datapoints
조회 수: 34 (최근 30일)
이전 댓글 표시
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
dpb
2021년 7월 29일
편집: dpb
2021년 7월 29일
What data points do you want added and how to look?
plot() works after where you are now...if you do it before the hold off command...aftter that and you'll wipe the slate clean first. Allthough line would work it's lower-level...
I'd also recommend to save the handles to the plot objects; never know when may want to add some niceties; keeping the handle around lets you do that -- plus also can use them for legend
채택된 답변
Dave B
2021년 7월 30일
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!)

댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
