필터 지우기
필터 지우기

Different boxplots with different colors

조회 수: 14 (최근 30일)
Nicole Andreoli
Nicole Andreoli 2020년 8월 27일
댓글: Nicole Andreoli 2020년 10월 1일
Hello. I created these 6 boxplot, now I would like to make the points of the 3 boxplots that have "left" in their name on the x-axis in one color, and the points of the 3 boxplots that have"right" in the name on the x-axis in another color.
Here is my code :
groupLR = [ ones(size(IntCapDataMedianSubtractNCL'));
2 * ones(size(IntCapDataMedianSubtractNCR'));
3 * ones(size(IntCapDataMedianSubtractPCL'));
4 * ones(size(IntCapDataMedianSubtractPCR'));
5 * ones(size(IntCapDataMedianSubtractTL'));
6 * ones(size(IntCapDataMedianSubtractTR'))];
IntCapDataMedianSubtractConLR=[IntCapDataMedianSubtractNCL'; IntCapDataMedianSubtractNCR'; ...
IntCapDataMedianSubtractPCL'; IntCapDataMedianSubtractPCR'; ...
IntCapDataMedianSubtractTL'; IntCapDataMedianSubtractTR']
j
IntCapDataMedianSubtractConL=[IntCapDataMedianSubtractNCL'; IntCapDataMedianSubtractPCL'; ...
IntCapDataMedianSubtractTL'];
IntCapDataMedianSubtractConR=[IntCapDataMedianSubtractNCR'; IntCapDataMedianSubtractPCR'; ...
IntCapDataMedianSubtractTR'];
figure
boxplot(IntCapDataMedianSubtractConLR,groupLR) ;
title('IntCapDataMedianSubtractConLR')
set(gca,'XTickLabel',{'NC Left','NC Right','PC Left','PC Right','T Left','T right'})
hold on
[C, ~, ic]=unique([groupLR],'stable');
scatter(ic,[IntCapDataMedianSubtractConLR],'filled','MarkerFaceAlpha',0.6','jitter','on','jitterAmount', 0.15);
xlabel('Condition');
ylabel('"Volume"');
Thank you for your help
  댓글 수: 2
dpb
dpb 2020년 8월 27일
Use the 'ColorGroup' and 'Colors' named value-pair arguments.
Nicole Andreoli
Nicole Andreoli 2020년 10월 1일
Thank you for your help

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

답변 (1개)

Sourabh Kondapaka
Sourabh Kondapaka 2020년 8월 31일
편집: Sourabh Kondapaka 2020년 8월 31일
Hi,
You can use findobj() function to access properties of the plot and to modify the plot you can use patch() to update the color of each box within the boxplot.
Here I am storing the x-axis tick labels in an array and then using it to generate colors based on the values in this array.
The two set of colors I’m choosing are:
• If the name on X-axis tick label is “left” I’m choosing red color.
• If the name on X-axis is tick label “right” I’m choosing blue color
Replace the below code with lines from “figure” to “set()” in the code you had uploaded.
xAxisTickLabels = {'NC Left','NC Right','PC Left','PC Right','T Left','T right'};
colorsForPlotting = cell(size(xAxisTickLabels));
for i = 1:size(xAxisTickLabels,2)
if contains(xAxisTickLabels{i},'Left')
colorsForPlotting{i} = 'r';
else
colorsForPlotting{i} = 'b';
end
end
boxplot(k, x);
set(gca,'XTickLabel',xAxisTickLabels)
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colorsForPlotting{j},'FaceAlpha',0.15);
end
Above lines of code should replace lines from “figure” to “set()” in the code you had added.
Below code gives a complete picture for random values.
k = randn(5,5) + 10;
xAxisTickLabels = {'NC Left','NC Right','PC Left','PC Right','T Left','T right'};
colorsForPlotting = cell(size(xAxisTickLabels));
x = 1:5;
for i = 1:size(xAxisTickLabels,2)
if contains(xAxisTickLabels{i},'Left')
colorsForPlotting{i} = 'r';
else
colorsForPlotting{i} = 'b';
end
end
boxplot(k, x);
set(gca,'XTickLabel',xAxisTickLabels)
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colorsForPlotting{j},'FaceAlpha',.15);
end
And the output is :
A similar question has been answered here
  댓글 수: 1
Nicole Andreoli
Nicole Andreoli 2020년 10월 1일
Thank you for your help, I managed to do it !
Do you also know how to change the colors of the points, instead of the color of the boxplot?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by