2017-06-22 25 views
2

我有两个单元阵列,每个单元有一个矩阵x =(10X5)。 x中的每一行都是一个数组(介于-1和1之间),其平均值为“m”,标准为“s”。现在我想使用MATLAB在矩形网格中表示这个矩阵,这样每个盒子都有一个平均值(橙色)和标准偏差(在平均值的任一侧填充红色),如示例所示。所以基本上应该有10X2的矩形网格(对应于10行和两个单元格)。有人可以帮助我吗?我在网上查询,但找不到任何东西。使用MATLAB的矩形网格

enter image description here

+1

'副区()'和'箱线图()' –

+0

看看[这里](https://stackoverflow.com/questions/39708175/hierarchically-grouped-boxplot)并回来一些代码开始 – EBH

+0

@EBH ...谢谢你的例子。我认为这不是一个箱子问题。基本上它需要绘制一个矩形网格,其中线条对应于用std dev填充的均值和面积。 – Shraddha

回答

1

您可以使用boxplot创建情节的初始结构,然后改变它们代表你想要什么。每个矩阵x被转换为一个网格图,并且这些图与subplot并排放置。

这里是一个简短的代码做你想要什么:

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 

它创建这样的:

enter image description here