How can I plot boxplots within a line graph?

16 views (last 30 days)
Zach
Zach on 27 Aug 2015
Commented: the cyclist on 27 Aug 2015
I have the above line plot that I want to add some box plots onto however the hold on function doesn't seem to work for box plots?
Currently I am plotting the data for the first graph, then using "hold on" and then trying to add several box plots using boxplot([data1,data2],[0,5]). I want this to plot the box plots for data1 and data2 at locations 0 and 5 along the x axis of the first line plot.
Any help is greatly appreciated.

Answers (1)

the cyclist
the cyclist on 27 Aug 2015
The way you have specified [0,5] does not correspond to the correct syntax of the boxplot command. (That will be interpreted as a grouping variable, which is probably why it is not working.)
I believe you want to specify your boxplot as
boxplot([data1,data2],'positions',[0,5])
  2 Comments
Zach
Zach on 27 Aug 2015
Edited: the cyclist on 27 Aug 2015
Hey this worked! But it I think it broke the axis for the original plot? How can I change it so the x axis is back to using the data from the original plot rather than the box plots?
The code I'm using to add the box plots is as follows:
hold on;
x1 = VarName2;
x2 = VarName4;
x3 = VarName6;
x = [x1;x2;x3];
g = [100*ones(size(x1)); 500*ones(size(x2)); 1000*ones(size(x3))];
boxplot(x,g,'positions',[100,500, 1000],'plotstyle','compact');
the cyclist
the cyclist on 27 Aug 2015
It's possible that simply doing the boxplots first, then the other, is the simplest way.
If that does not work, then after you do the first plot (but before the boxplot), do
xlim = get(gca,'XLim')
and then after the boxplot, do
set(gca,'XLim',xlim)
and similar for y if you need to.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!