MATLAB图形中的多个(平行)箱形图 [英] MATLAB Multiple(parallel) box plots in single figure

查看:1253
本文介绍了MATLAB图形中的多个(平行)箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the boxplot function in MATLAB. I need to plot boxplots for 6 different datasets for 6 'XTicks' i.e each tick in the x axis should contain 6 corresponding boxes, whiskers, median lines and set of outliers within it's domain. I tried manipulating the 'XTick' property by setting offsets for each variable, but it doesn't apply for boxplot() as it would for a normal plot(). I'm also not able to add legends.

等效于我的问题的3变量如下:

A 3 variable equivalent of my problem would like the following:

以下是需要修改的代码段

The following is the code snippet that needs to be modified

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

任何帮助将不胜感激!

推荐答案

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

I've made some modifications to your code. I've tested this in 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:numel(labels))+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) numel(labels)+legWidth+2*delta(N)]) %// adjust x limits, with room for legend

legend(legendEntries);

这篇关于MATLAB图形中的多个(平行)箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆