Grouped bar graph with individual datapoints

조회 수: 34 (최근 30일)
Varghese
Varghese 2021년 7월 29일
답변: Dave B 2021년 7월 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
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
Varghese
Varghese 2021년 7월 30일
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.

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

채택된 답변

Dave B
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개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by