MATLAB单个图中的多个(并行)箱形图

时间:2015-03-19 22:06:41

标签: matlab matlab-figure boxplot

我在MATLAB中使用boxplot函数。我需要为6个XTicks绘制6个不同数据集的箱线图,即x轴上的每个刻度应该包含6个相应的框,晶须,中线和它的域内的异常值集。我尝试通过为每个变量设置偏移来操纵'XTick'属性,但它不适用于boxplot(),因为它适用于普通的plot()。我也无法添加传说。

我的问题的3个变量等同于以下内容:

enter image description here

编辑:

以下是需要修改的代码段

TreadmillData = randi([20,200],69,6);
Speeds = {'1.5mph' '2.5mph' '3.5mph' '4.5mph' '5.5mph' '6.5mph'};
DeviceColors = {'r' 'g' 'c' [0.5 0 0.5] 'b' [1 0.5 0]};
Pedometer1 = TreadmillData(1:7:end,:);
Pedometer2 = TreadmillData(2:7:end,:);
Pedometer3 = TreadmillData(3:7:end,:);
Pedometer4 = TreadmillData(4:7:end,:);
Pedometer5 = TreadmillData(5:7:end,:);
Pedometer6 = TreadmillData(6:7:end,:);

GroupedData = {Pedometer1 Pedometer2 Pedometer3 Pedometer4 Pedometer5 Pedometer6}; 

legendEntries = {'dev1' 'dev2' 'dev3' 'dev4' 'dev5' 'dev6'};

figure;
Xt = 20:20:120;
Xt_Offset = [-15,-10,-5,5,10,15];

for i=1:6 
    boxplot(GroupedData{i},'Color',DeviceColors{i});
    set(gca,'XTick',Xt+Xt_Offset(i));
    if i==3
        set(gca,'XTickLabel',Speeds);
    end
    hold on;
end
xlabel('Speed');ylabel('Step Count'); grid on;
legend(legendEntries);

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

这是绘制几个boxplot的解决方案。您必须将所有数据分组到一个矩阵中,每个组由Nan列分隔。之后,您只需使用 ad-hoc 选项(例如boxplotcolors)绘制单个常规labels

enter image description here

以下示例使用2组3,即7列。 4个第一行数据:

    0.6993    0.0207   -0.7485       NaN    0.5836   -0.1763   -1.8468
   -0.0494   -1.5411    0.8022       NaN    2.7124   -0.0636   -2.3639
    0.9134    0.7106   -0.1375       NaN   -0.2200   -0.2528   -0.8350
   -0.5655    1.3820    0.6038       NaN   -0.7563   -0.9779    0.3789

代码:

figure('Color', 'w');
c = colormap(lines(3));


A = randn(60,7);        % some data
A(:,4) = NaN;           % this is the trick for boxplot
C = [c; ones(1,3); c];  % this is the trick for coloring the boxes


% regular plot
boxplot(A, 'colors', C, 'plotstyle', 'compact', ...
    'labels', {'','ASIA','','','','USA',''}); % label only two categories
hold on;
for ii = 1:3
    plot(NaN,1,'color', c(ii,:), 'LineWidth', 4);
end

title('BOXPLOT');
ylabel('MPG');
xlabel('ORIGIN');
legend({'SUV', 'SEDAN', 'SPORT'});

set(gca, 'XLim', [0 8], 'YLim', [-5 5]);

答案 1 :(得分:3)

我对您的代码进行了一些修改。我在R2014b中对此进行了测试。

TreadmillData = randi([20,200],69,6);
Speeds = {'1.5mph' '2.5mph' '3.5mph' '4.5mph' '5.5mph' '6.5mph'};
DeviceColors = {'r' 'g' 'c' [0.5 0 0.5] 'b' [1 0.5 0]};
Pedometer1 = TreadmillData(1:7:end,:);
Pedometer2 = TreadmillData(2:7:end,:);
Pedometer3 = TreadmillData(3:7:end,:);
Pedometer4 = TreadmillData(4:7:end,:);
Pedometer5 = TreadmillData(5:7:end,:);
Pedometer6 = TreadmillData(6:7:end,:);

GroupedData = {Pedometer1 Pedometer2 Pedometer3 Pedometer4 Pedometer5 Pedometer6}; 

legendEntries = {'dev1' 'dev2' 'dev3' 'dev4' 'dev5' 'dev6'};

N = numel(GroupedData);
delta = linspace(-.3,.3,N); %// define offsets to distinguish plots
width = .2; %// small width to avoid overlap
cmap = hsv(N); %// colormap
legWidth = 1.8; %// make room for legend

figure;
hold on;

for ii=1:N %// better not to shadow i (imaginary unit)
    %if ii~=ceil(N/2)
    %    labels = repmat({''},1,N); %// empty labels
    %else
        labels = Speeds; %// center plot: use real labels
    %end
    boxplot(GroupedData{ii},'Color', DeviceColors{ii}, 'boxstyle','filled', ...
        'position',(1:N)+delta(ii), 'widths',width, 'labels',labels)
        %// plot filled boxes with specified positions, widths, labels
    plot(NaN,1,'color',DeviceColors{ii}); %// dummy plot for legend
end
xlabel('Speed'); ylabel('Step Count'); grid on;
xlim([1+2*delta(1) N+legWidth+2*delta(N)]) %// adjust x limits, with room for legend

legend(legendEntries);

enter image description here