Question

I have two 1x5 vectors as follows.

A=[1 2 3 4 5]
B=[7 8 9 10 11]

Note that their entry values have no overlapping at all. I wish to have two box plots that lie horizontally at the same height of the y-axis, i.e. side by side, in one figure.

I learn that

x = rand(5,1);
y = rand(10,1);
z = rand(15,1);
group = [repmat({'First'}, 5, 1); repmat({'Second'}, 10, 1); repmat({'Third'}, 15, 1)];
boxplot([x;y;z], group, 'orientation', 'horizontal')

is able to draw three box plots together in one figure. However, the three box plots lie on three different y-axis "levels".

How may I do this?

Was it helpful?

Solution

Use the position property:

A = [1 2 3 4 5];
B = [7 8 9 10 11];


group = [repmat({'First'}, 5, 1); repmat({'Second'}, 5, 1)];
         boxplot([A';B'], group, 'orientation', 'horizontal','positions',[1 1])

As you can see, the y-labels are screwed up and manual labeling is required.

enter image description here

... which is quite easy to solve by just substituting {'Second'} with {' '}. You could use annotations instead for manual labeling.

group = [repmat({'boxplots'}, 5, 1); repmat({' '}, 5, 1)];
         boxplot([A';B'], group, 'orientation', 'horizontal','positions',[1 1])
annotation('textbox', [0.35,0.8,0.07,0.06],...
           'String', 'First','LineStyle','none');
annotation('textbox', [0.7,0.8,0.11,0.06],...
           'String', 'Second','LineStyle','none');  

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top