Grouped bar graph with individual datapoints

191 ビュー (過去 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 Center および File ExchangeBar Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by