필터 지우기
필터 지우기

how to fill boxes in Boxplot with different colors

조회 수: 365 (최근 30일)
Poulomi Ganguli
Poulomi Ganguli 2018년 4월 4일
댓글: Haochen Qin 2023년 5월 27일
Hello, I would like to plot boxplot with each of the boxes with separate colors, I came across these set of code from this link: https://groups.google.com/forum/#!topic/comp.soft-sys.matlab/JFi976iIuZE However, with this link, all boxes are right now shaded with yellow. In contrast, I am interested in each of the boxes gets filled with different colors, for example, blue, red and gray. Any way, how to achieve this? Thanks,
  댓글 수: 1
BN
BN 2020년 4월 5일
편집: BN 2020년 4월 5일
Dear Poulomi
I have a same question, 2 years after you ... did you find any answer by the way?
regards

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

답변 (3개)

Ameer Hamza
Ameer Hamza 2020년 4월 5일
편집: Ameer Hamza 2020년 5월 12일
Following the method in link posted by Poulomi, you can get different colors like this
data = rand(100, 4);
x = 1:4;
colors = rand(4, 3);
boxplot(data, x);
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colors(j,:),'FaceAlpha',.5);
end
If you are using R2020a, then use the following code, which is robust as compared to the above version
data = rand(100, 4);
x = 1:4;
colors = rand(4, 3);
figure();
ax = axes();
hold(ax);
for i=1:4
boxchart(x(i)*ones(size(data(:,i))), data(:,i), 'BoxFaceColor', colors(i,:))
end
  댓글 수: 3
Amir Semnani
Amir Semnani 2021년 12월 13일
Great! Thanks alot
Alberto Acri
Alberto Acri 2022년 4월 26일
How can I modify your code:
data = rand(100, 4);
x = 1:4;
colors = rand(4, 3);
boxplot(data, x);
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),colors(j,:),'FaceAlpha',.5);
end
To manually set the color of each boxplot?
Thanks!!

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


Mehri Mehrnia
Mehri Mehrnia 2021년 11월 7일
I want facecolor with command "boxplot" not "boxchart". Can anyone help?
The reason that I use boxplot, it's more handy for "legend"

J Culp
J Culp 2023년 2월 8일
편집: J Culp 2023년 2월 8일
Reviving this thread with another approach to coloring the boxes generated by boxplot.m rather than boxchart.m, in the 'traditional' 'PlotStyle' (with 'outline' 'BoxStyle'). I am using R2021b.
The solution is a bit hacky and you will probably need to put in some legwork to adapt it to your application. All you need to do to make changes is explore the dot properties of the figure you are working with. I did this by opening the Property Inspector in the figure and typing dot indexed commands in the Command Window until I found the properties for the boxplot lines.
I started with this thread and made adaptations based on what I needed: https://stackoverflow.com/questions/29155899/matlab-multipleparallel-box-plots-in-single-figure.
This is not a robust solution but it should get you going in the right direction.
groups = 6; samples = 24;
dataType1 = randi(2,samples,groups); %just some random 24x6 data
dataType2 = randi(4,samples,groups); % e.g., 6 groups of 24 samples, 3 bars per group
dataType3 = randi(6,samples,groups);
group_labels = {'20','30','40','60','80','100'}; % x-axis labels
% MATLAB yellow, blue, red
colors = {[0.9290 0.6940 0.1250] [0 0.4470 0.7410] [0.6350 0.0780 0.1840]};
GroupedData = {dataType1 dataType2 dataType3};
% legendEntries = {'data1' 'data2' 'data3'}; %if you want a legend
% copied directly from the stackoverflow thread
N = numel(GroupedData);
delta = linspace(-.3,.3,N); %// define offsets to distinguish plots
width = .2; %// small width to avoid overlap
fig = figure; hold on;
for i = 1:N
labels = group_labels;
boxplot(GroupedData{i},'Color', colors{i}, 'boxstyle','outline', ...
'position',(1:numel(labels))+delta(i), 'widths',width, 'labels',labels);
%// plot filled boxes with specified positions, widths, labels
% get the Line (Box) array elements from within Figure > Axes > Group > Line.
% will need to change indices in the final .Children() depending on your data
boxes = fig.Children.Children(1,1).Children( 13:18 );
for j = 1:length(boxes) % draw a colored patch behind each bar
patch( boxes(j).XData, boxes(j).YData, colors{i},'FaceAlpha',.5,'EdgeAlpha',0.3);
end
% plot(NaN,1,'color',colors{i}); %// dummy plot for legend (if you want a legend)
end
grid on;
Legwork: Run (highlight and press F9)
>> fig.Children.Children(1,1).Children
and locate the entries that say Line (Box). These are what you should replace the 13:18 indices shown in the example above with. There is probably a very simple way to make this work for arbitrary datasets, but I just wanted to quickly share this approach.
  댓글 수: 1
Haochen Qin
Haochen Qin 2023년 5월 27일
Amazing!This method is very useful. But when I want to do 4 sets of boxplots, it will be difficult to achieve. I'm not very clear about some of the parameters. Can you give me some pointers where, besides the data entry, the changes should be made?

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by