Different boxplots with different colors
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Sourabh Kondapaka
el 31 de Ag. de 2020
Editada: Sourabh Kondapaka
el 31 de Ag. de 2020
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
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!