Grouped bar graph with scatter plot

조회 수: 4 (최근 30일)
CR
CR 2020년 6월 18일
편집: Adam Danz 2020년 6월 23일
This code is close but getting the SEM over the first and last bars. Does anyone have any suggestions for the scatter code?
model_series=[nanmean(tmpdata1aS_Mean) nanmean(all_dyn1l1p0aS_Pre); nanmean(tmpdata1aS_MeanP) nanmean(all_dyn1l1p0aS_Post)];
model_error=[tmpdata1aS_SEM dyn1l1p0aS_PreSEM; tmpdata1aS_SEMP dyn1l1p0aS_PostSEM];
model_scatter=[tmpdata1aS_Mean all_dyn1l1p0aS_Pre; tmpdata1aS_MeanP all_dyn1l1p0aS_Post];
b=bar(model_series, 'grouped');
hold on
ngroups = size(model_series, 1);
nbars = size(model_series, 2);
groupwidth = min(0.8, nbars/(nbars + 1.5));
% Based on barweb.m by Bolu Ajiboye from MATLAB File Exchange
for i = 1:nbars
% Calculate center of each bar
x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
errorbar(x, model_series(:,i), model_error(:,i), 'k', 'linestyle', 'none');
scatter(x(i).* ones(length(model_scatter(i,:)),1),model_scatter(i,:),'*')
end
hold off
Thoughts?
  댓글 수: 2
Adam Danz
Adam Danz 2020년 6월 18일
편집: Adam Danz 2020년 6월 18일
"Does anyone have any suggestions for the scatter code?"
Could you describe what's wrong and what you'd like to see? It would also be helpful to know the size of your variables.
CR
CR 2020년 6월 18일
Yes. I would like the SEMs (asteriks) in model_scatter to be centered over the corresponding bar means (in model_series), similar to the location of the errorbars. There are 5-6 data points in each. Does this make sense?

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

답변 (1개)

Adam Danz
Adam Danz 2020년 6월 18일
편집: Adam Danz 2020년 6월 18일
Here are some demo you can follow to get the x-coordinate of the center of each bar. Any one of them should be sufficient.
Use the x-coordinates to specify the location of the scatter points.
  댓글 수: 3
Adam Danz
Adam Danz 2020년 6월 18일
편집: Adam Danz 2020년 6월 18일
It looks like you're looping over the bar-groups. If 'x' defines the center of each bar within group i, then x would have 2 values since there are two groups. model_scatter(i,:) is a vector. How do you decide which bar those points are plotted to?
Don't forget we can't run your code without inputs.
Adam Danz
Adam Danz 2020년 6월 18일
편집: Adam Danz 2020년 6월 23일
You probably don't need a loop. Why not just compute all x-values to all bars in 1 line of code, then add the errorbars in 1 line of code, then add the scatter points in 1 line of code? Why loop over the bar-groups. I think that complicates things.

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by