Colorfill the boxes in a boxplot in Matlab
Asked Answered
E

2

1

Is there a way to fill the boxes in a boxplot in Matlab?

I was able to change the color of the boundaries of the boxes using the colorgroup option of the boxplot function (http://www.mathworks.com/help/stats/boxplot.html), but could not find any option to change the or fill the color of the space inside box itself.

Edit: So, I went through the code at the link (http://www.mathworks.com/matlabcentral/newsreader/view_thread/300245) pointed out user1929959 in the comments. However, i am new to Matlab and I would really appreciate a brief explanation of what the code does. Here is the code from that link:

load carsmall
 boxplot(MPG,Origin)
 h = findobj(gca,'Tag','Box');
 for j=1:length(h)
    patch(get(h(j),'XData'),get(h(j),'YData'),'y','FaceAlpha',.5);
 end

I am also open to other solutions. Thanks.

Esemplastic answered 28/2, 2013 at 0:30 Comment(1)
See mathworks.com/matlabcentral/newsreader/view_thread/300245Eudemonism
S
2

With FINDOBJ function you search for graphic objects with tag equals to 'Box' in the current axes (gca = get current axis handle).

Tags for all objects in boxplot you can find in the official MW documentation (just before examples): http://www.mathworks.com/help/stats/boxplot.html

FINDOBJ returns handles to all objects it found into variable h, which is double array. You use the handle to modify the object properties. You can see all properties for a given handle with get(h(1)) or inspect(h(1)).

For example you can set line width:

set(h,'LineWidth',3)

Since box is a line object it doesn't have FaceColor or FaceAlpha (transparancy) properties as for patch, so you cannot color it directly. You have to draw patches over it with yellow color (set by 'y' parameter) and 0.5 transparancy. You get XData and YData properties to get the patch coordinates. See here for all patch properties.

Again if you don't know what some function does, always check matlab documentation with help function_name or doc function_name.

Sacrilege answered 28/2, 2013 at 3:34 Comment(0)
S
0

I know this is an old question, but it didn't seem to have a satisfactory answer yet - here is an example code inspired from the poster's edit which does the job:

data_to_plot = randn(10,2); % 2 columns of random numbers, each column will be shown in a boxplot
these_colours = [[0.9290 0.6940 0.1250];[0.4940 0.1840 0.5560]]; % yellow and purple
    
% This will plot the boxplot outlines in the desired colours
these_boxes = boxplot(data_to_plot,  'Colors',  these_colours);

%% This is where we fill the inside of boxes:
% This gets the box object references
box_objects = findobj(gca, 'Tag', 'Box');
% and for each of them creates a 'patch' object of the same colour as the
% box object
for j=1:length(box_objects)
    % note: the FaceAlpha parameter sets the transparency of the patch
    patch(get(box_objects(j),'XData'),get(box_objects(j),'YData'),'y', ...
    'FaceAlpha',.5,'FaceColor', get(box_objects(j), 'Color'), ...
    'EdgeColor', get(box_objects(j), 'Color'));
end
%%
    
% Bonus: if you want to change other attributes of the box, for example the
% line width:
set(these_boxes(1:6,:), 'LineWidth', 2);

Hope this helps! - Leonos

Supralapsarian answered 19/4, 2023 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.