Different boxplots with different colors

14 次查看(过去 30 天)
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

回答(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