Search code examples
matlabmatlab-figureboxplot

MATLAB Multiple(parallel) box plots in single figure


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.

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

enter image description here

Edit:

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

Any help would be appreciated!


Solution

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

    enter image description here