Adding a scatter of points in different colour to a boxplot

I would like to add the mean value of each subject (n=9) to my boxplot but for each subject I would like to use a different colour.
Does anyone know how to do this?
I managed to put the points but all of them appear in same colour. Here is my code:
x = abs(randn(9,1));
y = abs(randn(9,1));
group = [ ones(size(x));
2 * ones(size(y))];
boxplot([x; y],group)
h = boxplot([x;y],group)
set(h,{'linew'},{2})
set(gca,'XTickLabel', {'X'; 'Y'})
allData = {x;y};
h = boxplot([allData{:}],group);
set(h, 'linewidth' ,2)
set(gca,'XTickLabel', {'X'; 'Y'})
hold on
xCenter = 1:numel(allData);
spread=0.2;
for i = 1:numel(allData)
plot(rand(size(allData{i}))*spread -(spread/2) + xCenter(i), allData{i}, 'mo','linewidth', 2)
end

댓글 수: 5

Could you please provide a hint how to it?
Oh. That wasn't the question asked... :)
Use scatter instead of plot
Adam Danz
Adam Danz 2020년 6월 29일
편집: Adam Danz 2020년 6월 29일
dpb gave you the same suggestion I suggested ~4 hrs earlier. It must be a good suggestion :)

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

 채택된 답변

Adam Danz
Adam Danz 2020년 6월 29일
편집: Adam Danz 2020년 6월 30일
The code in the question is from this answer.
To use different colors for each group of points, you can plot the points within a loop or you can use scatter() which allows you to assign a color for each point.
It looks like you tried the loop method but you're using the same color on each iteration.
Here's what you need to add/change,
colors = jet(numel(allData));
for i = 1:numel(allData)
plot(rand(size(allData{i}))*spread -(spread/2) + xCenter(i), allData{i}, 'o','linewidth', 2, 'Color', colors(i,:))
end
To use different colors for each point within a group, use scatter()
colors = jet(numel(allData{i}));
for i = 1:numel(allData)
scatter(rand(size(allData{i}))*spread -(spread/2) + xCenter(i), allData{i}, ...
80, colors, 'filled')
end

댓글 수: 3

DM
DM 2020년 6월 30일
편집: DM 2020년 6월 30일
Thanks a lot for your answer. I tried the loop you suggested but I have different colour in the two boxplots.
What I would like to do is to have 9 different colours in the X boxplot and the corresponding 9 colours in Y boxplot. The X and Y plot is data from pre and post surgery and I would like to plot the data for each patient in different colour.
x = abs(randn(9,1));
y = abs(randn(9,1));
group = [ ones(size(x));
2 * ones(size(y))];
boxplot([x; y],group)
h = boxplot([x;y],group)
set(h,{'linew'},{2})
set(gca,'XTickLabel', {'X'; 'Y'})
allData = {x;y};
h = boxplot([allData{:}],group);
set(h, 'linewidth' ,2)
set(gca,'XTickLabel', {'X'; 'Y'})
hold on
xCenter = 1:numel(allData);
spread=0.2;
colors = jet(numel(allData));
for i = 1:numel(allData)
plot(rand(size(allData{i}))*spread -(spread/2) + xCenter(i), allData{i}, 'o','linewidth', 2, 'Color', colors(i,:))
end
I see.
I've updated my answer.
Now it works perfectly. Thanks a lot :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

질문:

DM
2020년 6월 29일

댓글:

DM
2020년 6월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by