绘制分组箱线图Matlab的最有效方法 [英] Most efficient way of drawing grouped boxplot matlab

查看:1840
本文介绍了绘制分组箱线图Matlab的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个向量:Y=rand(1000,1)X=Y-rand(1000,1)ACTid=randi(6,1000,1). 我想按Y和X的组(对应于ACTid)的组值1:6来创建箱形图.

I have 3 vectors: Y=rand(1000,1), X=Y-rand(1000,1) and ACTid=randi(6,1000,1). I'd like to create boxplots by groups of Y and X corresponding to their group value 1:6 (from ACTid).

这是临时的,看上去很讨厌

This is rather ad-hoc and looks nasty

for ii=
dummyY(ii)={Y(ACTid==ii)};
dummyX(ii)={X(ACTid==ii)}
end

现在我将数据保存在一个单元格中,但无法计算出如何在箱图中将其分组.有什么想法吗?

Now I have the data in a cell but can't work out how to group it in a boxplot. Any thoughts?

我发现aboxplot 功能看起来像这样,但我没有想要那个,我想要内置的boxplot函数,因为我将其转换为matlab2tikz,而这个函数做得不好.

I've found aboxplot function that looks like this but I don't want that, I'd like the builtin boxplot function because i'm converting it to matlab2tikz and this one doesn't do it well.

编辑

感谢Oleg:我们现在有一个分组的箱线图...但是标签全都是歪斜的.

Thanks to Oleg: we now have a grouped boxplot... but the labels are all skew-whiff.

xylabel = repmat({'Bleh','Blah'},1000,1); % need a legend instead, but doesn't appear possible
boxplot([Y(:,end); cfu], {repmat(ACTid,2,1), xylabel(:)} ,'factorgap',10,'color','rk')
set(gca,'xtick',1.5:3.2:50)
set(gca,'xticklabel',{'Direct care','Housekeeping','Mealtimes','Medication','Miscellaneous','Personal care'})
>> ylabel('Raw CFU counts (Y)')

如何添加图例?

推荐答案

一种两行方法(尽管如果要保留两行xlables并将它们放在第一行的中心,那将是骇人听闻的):

A two-line approach (although if you want to retain two-line xlables and center those in the first line, it's gonna be hackish):

Y     = rand(1000,1);
X     = Y-rand(1000,1);
ACTid = randi(6,1000,1);

xylabel = repmat('xy',1000,1);
boxplot([X; Y], {repmat(ACTid,2,1), xylabel(:)} ,'factorgap',10)

结果:

编辑

将标签居中...

% Retrieve handles to text labels
h = allchild(findall(gca,'type','hggroup'));

% Delete x, y labels
throw = findobj(h,'string','x','-or','string','y');
h     = setdiff(h,throw);
delete(throw);

% Center labels
mylbl  = {'this','is','a','pain','in...','guess!'};
hlbl   = findall(h,'type','text');
pos    = cell2mat(get(hlbl,'pos'));

% New centered position for first intra-group label
newPos = num2cell([mean(reshape(pos(:,1),2,[]))' pos(1:2:end,2:end)],2);
set(hlbl(1:2:end),{'pos'},newPos,{'string'},mylbl')

% delete second intra-group label
delete(hlbl(2:2:end))

导出为.png会引起问题...

Exporting as .png will cause problems...

这篇关于绘制分组箱线图Matlab的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆