Borrar filtros
Borrar filtros

how to fill boxes in Boxplot with different colors

452 visualizaciones (últimos 30 días)
Poulomi Ganguli
Poulomi Ganguli el 4 de Abr. de 2018
Comentada: Haochen Qin el 27 de Mayo de 2023
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 comentario
BN
BN el 5 de Abr. de 2020
Editada: BN el 5 de Abr. de 2020
Dear Poulomi
I have a same question, 2 years after you ... did you find any answer by the way?
regards

Iniciar sesión para comentar.

Respuestas (3)

Ameer Hamza
Ameer Hamza el 5 de Abr. de 2020
Editada: Ameer Hamza el 12 de Mayo de 2020
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 comentarios
Amir Semnani
Amir Semnani el 13 de Dic. de 2021
Great! Thanks alot
Alberto Acri
Alberto Acri el 26 de Abr. de 2022
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!!

Iniciar sesión para comentar.


Mehri Mehrnia
Mehri Mehrnia el 7 de Nov. de 2021
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 el 8 de Feb. de 2023
Editada: J Culp el 8 de Feb. de 2023
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 comentario
Haochen Qin
Haochen Qin el 27 de Mayo de 2023
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?

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by