![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175110/image.png)
How to plot using "Hierarchically grouped boxplot"?
4 views (last 30 days)
Show older comments
I would like to draw a boxplot with two sets of data to compare. I am willing to use "Hierarchically grouped boxplot" a function which I found on the mathworks.com in the file exchange. I could just plot one set of my data using this function. I was wondering how I can use this function to plot two sets of data together. I drew the second set of data in red one by hand to show what I am trying to plot!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/156651/image.gif)
0 Comments
Answers (1)
Kelly Kearney
on 26 Sep 2016
You can do something similar to this with my boxplot2.m function (sorry, the GitHub page isn't well-documented... I'll correct that shortly). It's specifically designed to plot boxplots using a similar layout as grouped bar plots:
% Some fake data
data1 = randn(6,5,100); % # groups x # boxes in each group x # data points
data2 = randn(6,3,100)+ 5;
x = (2001:2006)';
# Plot boxplots
h1 = boxplot2(data1, x);
h2 = boxplot2(data2, x);
# Change colors from default
set(h1.box, 'color', 'b');
set([h1.lwhis h1.uwhis], 'linestyle', '-', 'color', 'b');
set(h1.med, 'color', 'b');
set(h1.out, 'marker', '.', 'color', 'b');
set(h2.box, 'color', 'r');
set([h2.lwhis h2.uwhis], 'linestyle', '-', 'color', 'r');
set(h2.out, 'marker', '.', 'color', 'r');
set(gcf, 'color', 'w');
export_fig('boxplot2ex', '-png', 'nocrop');
set(h2.med, 'color', 'r');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175110/image.png)
4 Comments
Hydro
on 28 Dec 2017
Hello Kelly, I am trying to adapt your code of boxplot2 for my data but all in vain. The first 5 columns represent data for RCP4.5 while the next 5 RCP8.5. Row 1:360 is monthly data from 2011-2040, and the rest for 2041-2070. I want to boxplot the 2011-2040 of RCP45 vs RCP85 such that column1 of RCP45 is plotted adjacent to column1 of RCP85 and likewise for the rest columns. On the right side of the figure, I want to plot the data same way for the year 2041-2070. Here is what i thought of doing manually and then tried to used your code but couldnt get it.
Fake_Data=rand(720,10);
x1 = {'RCP45_1' 'RCP45_2' 'RCP45_3' 'RCP45_4' 'RCP45_5'}
x2 = {'RCP85_1' 'RCP85_2' 'RCP85_3' 'RCP85_4' 'RCP85_5'}
h1=boxplot2(Fake_Data(1:360,x1));
h2=boxplot2(Fake_Data(1:360,x2));
% the following should come on the right side of the figure
h3=boxplot2(Fake_Data(361:end,x1));
h4=boxplot2(Fake_Data(361:end,x2));
Any help would be highly appreciated.
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!