Вопрос

I am trying to plot a set of horizontal and vertical boxplots on the same axes in Matlab R2011b. Using the usual hold on command does not seem to work; only the second set of boxplots is shown. My code is as follows:

bv = boxplot(x,yGrp,'orientation','vertical');
hold on
bh = boxplot(y,xGrp,'orientation','horizontal','position',yPos);

yGrp and xGrp, the grouping index variables are specifically set to be in the range of the y and x datasets, respectively, so that the two plots should naturally have a similar set of values. That is, my x variables spans the range 0-0.05 and my y-variable spans the range 0-1, so yGrp contains a set of categorical numbers between 0 and 1 and xGrp contains bin numbers spanning the range 0-0.05. Similarly, yPos is chosen to span the expected 0-1 range of the vertical axes.

Removing the 'position' argument in the second boxplot call or trying to use simple integer variables does not help. If plotting in separate windows my boxplots look good, but I cannot combine them. What am I doing wrong?

Это было полезно?

Решение

The problem is in axes limits (xlim and ylim). Briefly your 1st boxplots exist but hidden out of axes.

When you plot the second boxplot, it set its own limits ignoring the 1st boxplot. In addition, boxplot by default sets position (y values for horizontal orientation) as 1:number_of_groups, but label them according to your groups. So the ylim will be [0.5 number_of_groups+0.5]. Since your x values are between 0 and 0.05, they are not visible.

As a solution set the limits manually considering this boxplot behavior:

x = rand(10,1)/20;
xg = randi(2,10,1)/40;
y = rand(10,1);
yg = randi(2,10,1)/2;
bv = boxplot(x,xg,'orientation','vertical');
xlim manual
hold on
bh = boxplot(y,yg,'orientation','horizontal');
hold off
xlim([0 2.5])
ylim([0 2.5])

When you set position parameter it determines the values instead of 1:number_of_groups. You can set them close to the range of x.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top