如何在一个图中水平绘制两个箱形图并在同一高度绘制?

时间:2014-03-22 02:38:22

标签: matlab plot matlab-figure

我有两个1x5向量,如下所示。

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

请注意,他们的输入值根本没有重叠。我希望在一个图中水平位于y轴的相同高度,即并排两个箱形图

我了解到

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')

能够在一个图中绘制三个箱形图。但是,三个箱形图位于三个不同的y轴“水平”上。

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

使用position属性:

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])

如您所见,y标签已拧紧,需要手动标记。

enter image description here

...只需用{'Second'}代替{' '}即可轻松解决。您可以使用annotations代替手动标记。

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

相关问题