Boxplot with double y-axis

22 views (last 30 days)
sunny kant
sunny kant on 24 Aug 2022
Answered: sunny kant on 24 Aug 2022
Hello All, I want to plot a boxplot with a double y-axis (reference figure is attached). Actually, I have two sets of variables. One variable value will display on the left side and another variable value will display on the right of the y-axis.

Accepted Answer

Aditya
Aditya on 24 Aug 2022
Edited: Aditya on 24 Aug 2022
MATLAB doesn't support drawing two boxplots as you are suggesting. But, you can use a work around.
The idea is to create empty box plots.
load carsmall
yyaxis left
boxplot([Acceleration nan(100,1)])
yyaxis right
boxplot([nan(100,1) Horsepower])
You can see that [Acceleration nan(100,1)] creates a 100x2 matrix with second column as nan.
  • When I use boxplot for left axis, it plots two curves at x-location 1 and 2 for each column. As the second column is nans, second boxplot is empty.
  • Now, for second plot, I want location-1 to be empty (as I already have plotted acceleration on that), so I use [nan(100,1) Horsepower], which will draw Horsepower at x-location 2.
For your refereimage, you will need to do something like this
yyaxis left
boxplot([l1 nan(100,1) l2 nan(100,1) l3 nan(100,1)])
yyaxis right
boxplot([nan(100,1) r1 nan(100,1) r2 nan(100,1) r3])
This is not what you are looking for but I think closest:

More Answers (4)

Aditya
Aditya on 24 Aug 2022
Edited: Aditya on 24 Aug 2022
Hi,
You can use the yyaxis command.
The key idea is to activate the axis that you want to plot. Suppose I have two ys : y1 and y2, I want to plot y1 on the left side and y2 on the right side, I can do as shown here:
yyaxis left
plot(x,y1);
yyaxis right
plot(x,y2);
You can read more about it at the example shown here.

sunny kant
sunny kant on 24 Aug 2022
Thank you Aditya for your reply. This will work for the plot command. I am applying for the boxplot, it is not working.
  1 Comment
Aditya
Aditya on 24 Aug 2022
Edited: Aditya on 24 Aug 2022
I think it is working as intended, as there are two boxplots at the same 'x'.
A simple example is here
load carsmall
yyaxis left
boxplot(Acceleration)
yyaxis right
boxplot(Horsepower)
What you are looking for is a no superposition.

Sign in to comment.


sunny kant
sunny kant on 24 Aug 2022
Aditya, I have also tried this, but two variables (means two box) go inside each other. I want to separate it (image attached for your reference) because it can be two separate boxes for two variables.

sunny kant
sunny kant on 24 Aug 2022
Thank you Aditya, it worked for me.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!