File Exchange

image thumbnail

Hierarchically grouped boxplot

version (3.26 KB) by Matt Tearle
A modification to boxplot that shows the hierarchical relationships between the grouping variables.


Updated 01 Sep 2016

View License

HIERARCHICALBOXPLOT(X,GRP) creates a boxplot of the data in X, grouped according to the grouping variables represented by the columns of GRP. The groupings are shown hierarchically, with the groups in each column of GRP being shown as groups within the groups of the next column.
Known limitation: BOXPLOT readjusts the label positions whenever the figure is resized (including docking/undocking). HIERARCHICALBOXPLOT does *not*. Too much resizing may cause strange-looking label positions.

Cite As

Matt Tearle (2020). Hierarchically grouped boxplot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)


Hi Matt
this is exactly what I was looing for. However, my y1,y2,y3... are not of same lengths. How should I run this function then? Thanks for your help.

Matt Tearle

Hi Rita. Thanks for the feedback. Unfortunately, I built this on top of the regular boxplot function, which imposed a number of restrictions I couldn't find my way around, hence the error message about not being able to handle groups with no data. With some of the new data analysis functionality in MATLAB, I might be able to rebuild this function from scratch.

With respect to plotting two sets of data, what would your desired result be? Are you thinking of plotting x with groups g, then adding y with the same groups g? In which case, would you want the y boxes next to the x ones (in a different color or something)? Or directly on top (with some transparency, presumably)? Again, there's no good way to do this right now, but I'd like to consider enhancements if I rebuild it.


Thanks Matt, How I can add another set of data to the plot .I mean plot this box plot and then to compare I add "hold on" and plot another boxplot in the same graph


Thanks for the function.I got an error "Error using hierarchicalBoxplot (line 86)
Some of the group combinations had no data and hierarchicalBoxplot can't cope with that.

Matt Tearle

Hi Juan (& Arnold -- sorry, for some reason I never saw your comments before)

Thanks for your feedback. Unfortunately there's no easy way to change things because, to be blunt, my code is a pile of pretty hideous hacks on top of boxplot. You could certainly hack my hacks for a specific purpose, but I don't know if there's a good way to do so generally.

I like the idea of using color. Do you have an idea of what the function interface would look like for using color instead of separator lines? I'm wondering about an optional cell array of n strings (n = number of grouping variables) that could be something like 'color', 'backgroundcolor', 'line', or 'none'. But that way you couldn't specify *what* the colors were, unless you could give another array of colors. But that seems a bit tedious. Any thoughts?

Hi Matt,

Thank you for this code. I wonder if you could give me a hand with this. First, I would like to use background color instead of lines for separating my groups. Second, I would like to substract the first line of ticks. Let me try to be clearer with this part. Taking the plot you have as an example, I would like to substract "70 76 82 70 76 82 70 76 82" just from the ticks.

I really appreciate your help.



Hi Matt, I was wondering if you might have a chance to include coloring+legend as another dimension. Otherwise... using it every week. Regards Arnold


Hi Matt,
thanks for the function. This is really missing in Matlab.
A couple of thoughts that might make it even better:

The ability to plot the lowest hierarchy/dimension as different box-color, or maybe second y-axis. This is closely related to the second idea:
The ability to plot 2-y axis, differentiated by color as well. Many applications have two different variables you might wanna show.

3. It would be more usable if one could simply feed it a table. The new 'table' data type looks like it would for great with this.



Updated license

Copyright info

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

Inspired: Alternative box plot