File Exchange

image thumbnail


version 2.0.0 (360 KB) by Adam Danz
Group boxplots together along the x-axis with space between groups.


Updated 27 Apr 2021

View Version History

View License

x = {rand(100,2), rand(20, 2), rand(150,2)};

The lines above will create two groups of three boxplot boxes per group, with space separating the groups. You can control the amount of space between groups and you can add group labels.

See the example file for a variety of formatting options available. Most boxplot properties available in Matlab can be specified in this function, too. However, if you're working with boxplot's grouping variable 'g', use the syntax boxplot(x,g) along with the "Group Appearance" options described in Matlab's boxplot() documentation.

** Version 2.0**
As requested by several users, now you can set the color of boxplots by group or by members within each group (see example file).

Tested in Matlab r2016a, r2018b, and r2021a.

Cite As

Adam Danz (2021). boxplotGroup (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

Michele Nicastro

Diogo Gonçalves

Jyoti Singh

Can I add more subgroups in this code?

Ville Ahonen

Adam Danz

Thanks, Behzad Navidi. To control the sequence of colors used within each group, set the 'Colors' property (see Matlab's boxplot documentation) but include one additional color than the number of groups to account for the gap between groups. Example:
h = boxplotGroup({rand(100,4), rand(20,4)*.8, rand(1000,4)*1.2}, 'Colors', lines(4));

Alternatively, you can assign colors after the plot is created. Example:
set(h.axis.Children(2).Children,'color', 'm') % sets color for the 2nd set of boxplots

Setting colors *within* each group is not currently supported. Matlab's boxplot function also includes a 'ColorGroup' property that can be used with grouped boxplot created by Matlab's boxplot function but not currently supported by my function.

Please PM me using the [Contact] button in my profile if you have any further questions on this topic.


Dear @Adam Danz, Thank you so much for creating this useful and beneficial function; God bless you. Is it possible to have two different colors for each group (if I have two groups)??

Adam Danz

@Reinder, the demo in the function's documentation uses data with varying heights [100x4, 20x4, 1000x4] like you're describing with [n*3, m*3]. If you enter data with a varying number of columns, you'll get an error message that suggests padding the matrices containing fewer columns with NaN values. If that doesn't answer your question, please write to me directly using the [contact] button in my profile.

Reinder Dorman

Hey is possible to use this with unequal amounts of data within each subgroups? As in, let's say I want to have 2 groups with 3 bars in each group, with unequal amount the form of x would be x = [n*3, m*3]; but in this case we can't make a matrix with unequal numbers of columns in each row.

I think the GroupOrder only works for the primary, if i'm seeing it okay?


Easy to use if want to plot multiple boxplot together, especially for writing a paper

MATLAB Release Compatibility
Created with R2021a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!