Boxplot with double y-axis

34 次查看(过去 30 天)
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.

采纳的回答

Aditya
Aditya 2022-8-24
编辑:Aditya 2022-8-24
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:

更多回答(4 个)

Aditya
Aditya 2022-8-24
编辑:Aditya 2022-8-24
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 2022-8-24
Thank you Aditya for your reply. This will work for the plot command. I am applying for the boxplot, it is not working.
  1 个评论
Aditya
Aditya 2022-8-24
编辑:Aditya 2022-8-24
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.

请先登录,再进行评论。


sunny kant
sunny kant 2022-8-24
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 2022-8-24
Thank you Aditya, it worked for me.

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by