Plot 是指 MATLAB 中的分组箱线图?

Plot means over grouped boxplot in MATLAB?

我正在使用 multiple_boxplot 函数生成分组箱线图: http://au.mathworks.com/matlabcentral/fileexchange/47233-multiple-boxplot-m

但是,我想绘制均值而不是中位数。首先我尝试了一般方法:

plot([mean(x)],'dg'); 

但是没有用。我试图提取方法然后绘制它们,但这也不起作用。

m=[];
for i=1:max(group) 
idx=find(group==i);
m=[m nanmean(x(idx))];
end

boxplot(x,group, 'positions', positions);hold on 
plot([m],'dg')

我做错了什么?以及如何用每个箱线图绘制均值? 谢谢。

您可以执行以下操作:

在函数 multiple_boxplot 中将第 48 行更改为:

B = boxplot(x,group, 'positions', positions);

并将函数的header改为:

B = multiple_boxplot(data...

并保存函数文件。

这不会改变函数的工作方式,但会让您获得箱线图的句柄 (B)。

然后在您的代码中,像以前一样创建箱线图,但使用输出参数 B:

B = multiple_boxplot(data...);

并添加以下行:

% compute the mean by group:
M = cellfun(@mean,data);
% convert it to pairs of Y values:
M = mat2cell(repmat(M(:),1,2),ones(size(M,1),1),2);
% change the medians to means:
set(B(6,:),{'YData'},M)