Color each boxplot differently
130 次查看(过去 30 天)
显示 更早的评论
I found an example that allows you to color with random colors. However, I want to color the inside of each boxplot with a certain color.
a = magic(3);
boxplot(a)
i = 3; % number of boxplots
x = 1:i;
colors = rand(i, 3);
figure
boxplot(a, x,'Whisker', inf);
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colors(j,:),'FaceAlpha',.5);
end

I would also like to make the lines thicker. How can I do this?
0 个评论
回答(1 个)
Jon
2022-4-26
I think this should get you closer to what you want. You can select other colors using their rgb color codes, I just used red,green and blue in the example
a = magic(3);
boxplot(a)
i = 3; % number of boxplots
x = 1:i;
colors = [1 0 0;0 1 0;0 0 1];
figure
boxplot(a, x,'BoxStyle','outline','Colors',colors);
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),get(h(j),'Color'),'FaceAlpha',.5);
end
3 个评论
Jon
2022-4-26
If you are running R2020A or higher you may want to investigate boxchart. It seems to offer the options you want more directly
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
