2

I have two cell arrays and each cell has a matrix x = (10X5). Each row in x is an array (between -1 and 1) with a mean "m" and std "s". Now i want to represent this matrix in a rectangular grid using MATLAB such that each box has a mean (orange color) and std deviation (filled with red colour in either side of mean) as shown in the example. So basically there should be 10X2 rectangular grids (corresponding to 10 rows and two cells). Can someone please help me with this? I looked up online but could not find anything.

enter image description here

EBH
  • 10,350
  • 3
  • 34
  • 59
Shraddha
  • 183
  • 1
  • 2
  • 10
  • 1
    `subplot()` and `boxplot()` – Brendan Frick Jun 22 '17 at 17:13
  • Take a look [here](https://stackoverflow.com/questions/39708175/hierarchically-grouped-boxplot) and come back with some code to start with – EBH Jun 22 '17 at 21:35
  • @EBH... Thank you for the example. I think this is not a boxplot problem. Basically it requires to plot a rectangular grid with lines corresponding to the mean and area filling with std dev. – Shraddha Jun 23 '17 at 00:36

1 Answers1

1

You can use boxplot to create the initial structure of the plot, and then alter them to represent what you want. Each matrix x is converted to one grid plot, and the plots are placed side by side with subplot.

Here is a short code to do what you want:

A = {rand(10,5)*2-1,rand(10,5)*2-1}; % your cell array
for n = 1:numel(A)
    subplot(1,2,n)
    x = A{n};
    means = mean(x,2);
    stds = std(x,[],2);
    % create boxplot for all variables:
    bx = boxplot(x.','Orientation','horizontal');
    % remove what's unnecessary:
    delete(bx([1:4 7],:))
    % set the median to mean:
    set(bx(6,:),{'XData'},...
        mat2cell([means means],ones(size(x,1),1),2))
    set(bx(6,:),{'Color','LineWidth'},{[1 0.7 0],3})
    % set the interQ range to std:
    std_bounds = repmat(means,1,5)+bsxfun(@times,stds,[-1 1 1 -1 -1]);
    set(bx(5,:),{'XData'},mat2cell(std_bounds,ones(size(x,1),1),5))
    set(bx(5,:),'Color',[0.8 0 0])
    for k = 1:size(std_bounds,1)
        patch(std_bounds(k,:),get(bx(5,k),'YData'),[0.8 0 0],...
            'FaceAlpha',0.7,...
            'EdgeColor','none')
    end
    xlim([-1 1])
    ax = gca;
    ax.Children = ax.Children([end 1:end-1]);
    % create the grid:
    set(ax,{'YGrid','GridColor','GridAlpha','XTick','XAxisLocation','YTick'},...
        {'on','k',1,[-1 0 1],'top',(1:size(x,1))+0.5})
    % set the zero line:
    line(ax,zeros(size(x,1)+2,1),(0:size(x,1)+1).','LineStyle','--','Color','k')
    if n>1
        set(ax,'YTickLabel',[])
    end       
end

It creates this:

enter image description here

EBH
  • 10,350
  • 3
  • 34
  • 59