箱线图标签与组不匹配

Boxplot label doesn't match with groups

我有以下代码来绘制箱形图,但我不断收到错误消息 "There must be the same number of labels as groups or as the number of elements in X"。有人可以指出我的错误吗?谢谢。

  xyz = [1x160];
  xzy = [1x160];
  yzx = [1x160];
  yxz = [1x160];
  zxy = [1x160];
  zyx = [1x160];
    figure();
    boxplot([xyz, xzy, yxz, yzx, zyx, zxy],'notch', 'on','labels',  {'xyz','xzy','yxz','yzx','zyx','zxy'}, 'symbol', '')
    hold on
    ylim([0,30]);
    xlabel('Rotational Matrices')
    ylabel('fn(gamma)')
    grid();

您的矢量需要以正确的方式定向(注意 ' 转置):

xyz = linspace(1,160,160)';
xzy = linspace(1,160,160)';
yzx = linspace(1,160,160)';
yxz = linspace(1,160,160)';
zxy = linspace(1,160,160)';
zyx = linspace(1,160,160)';
figure();
boxplot([xyz, xzy, yxz, yzx, zyx, zxy],'notch', 'on','labels',  {'xyz','xzy','yxz','yzx','zyx','zxy'}, 'symbol', '')
hold on
% ylim([0,30]);
xlabel('Rotational Matrices')
ylabel('fn(gamma)')
grid();

这里的主要问题是您将向量连接成一个大向量。根据 boxplot 的文档:

If X is a matrix, there is one box per column; if X is a vector, there is just one box.

由于只有一个盒子,而您试图传递多个标签,调用将出错。

您想要做的是创建一个矩阵,其中每一列数据对应于您的向量。观察以下区别:

A = 1:4;
B = 5:8;

test1 = [A, B];
test2 = [A', B'];

>> test1

test1 =

     1     2     3     4     5     6     7     8

>> test2

test2 =

     1     5
     2     6
     3     7
     4     8