多个Boxplot,具有各种大小的数据集标签

时间:2015-09-30 12:04:50

标签: matlab dataset boxplot

我仔细阅读了帖子Most efficient way of drawing grouped boxplot matlab。 Oleg提供的代码:

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))

完美无缺。但是我对'句柄'结构有疑问。在我的情况下,我只有每个数字{1,2,3,4,5,6}的单个数据,所以我不需要xylabel我使用boxplot([X; Y], {repmat(ACTid,2,1) repmat('x',1000,1)} ,'factorgap',10)并且它工作正常。为什么我们需要使用x标签?如果我不使用它,变量h没有'数据源'类型,并被引用为'line'类型,因此用字符串替换数字不再起作用。有谁知道为什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你真的只是问如何将XTickLabel更改为某些字符串。如果是这样,这应该可以解决问题:

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

boxplot([X; Y],repmat(ACTid,2,1),'factorgap',10);
mylbl  = {'this','is','a','pain','in...','guess!'};
set(gca,'xticklabel',mylbl)

更多详情:

boxplot函数的第二个输入是分组变量。如果仅使用一个分组变量,boxplot只会更改XTickLabel本身以匹配分组。您可以在get(gca,'xticklabel')命令后面输入boxplot来查看此内容:

boxplot([X; Y],repmat(ACTid,2,1),'factorgap',10)
get(gca,'xticklabel')
ans = 
    '1'
    '2'
    '3'
    '4'
    '5'
    '6'

如果使用多个分组变量 - 例如两个 - boxplot必须是花哨的,以便使用两个分组标记您的箱图(请参见下图),因此,它将XTickLabel设置为空,然后插入一些text对象。使用两个分组变量后查看XTickLabel

boxplot([X; Y], {repmat(ACTid,2,1), xylabel(:)} ,'factorgap',10)
get(gca,'xticklabel')
ans =
     ''
%// It's empty!!!

这就是为什么你在使用两个分组变量之后找到text个对象,但是当你只使用一个变量时

Using two grouping variables