【问题标题】:How to draw two box plots horizontally and at the same height in one figure?如何在一个图中水平绘制两个相同高度的箱形图?
【发布时间】:2014-03-22 02:38:22
【问题描述】:

我有两个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 轴“水平”上。

我该怎么做?

【问题讨论】:

    标签: matlab plot matlab-figure


    【解决方案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 标签搞砸了,需要手动标注。

    ...这很容易解决,只需将{'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');  
    

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多