使用 MATLAB 的矩形网格
Rectangular grid using MATLAB
我有两个元胞数组,每个元胞都有一个矩阵 x = (10X5)。 x 中的每一行都是一个数组(介于 -1 和 1 之间),均值 "m" 和标准差 "s"。现在我想用 MATLAB 在矩形网格中表示这个矩阵,这样每个框都有一个平均值(橙色)和标准偏差(在平均值的两边填充红色),如示例所示。所以基本上应该有 10X2 的矩形网格(对应 10 行和两个单元格)。有人可以帮我吗?我在网上查了一下,但找不到任何东西。
您可以使用 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
它创建了这个:
我有两个元胞数组,每个元胞都有一个矩阵 x = (10X5)。 x 中的每一行都是一个数组(介于 -1 和 1 之间),均值 "m" 和标准差 "s"。现在我想用 MATLAB 在矩形网格中表示这个矩阵,这样每个框都有一个平均值(橙色)和标准偏差(在平均值的两边填充红色),如示例所示。所以基本上应该有 10X2 的矩形网格(对应 10 行和两个单元格)。有人可以帮我吗?我在网上查了一下,但找不到任何东西。
您可以使用 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
它创建了这个: