Most efficient way of drawing grouped boxplot matlab

前端 未结 2 1724
刺人心
刺人心 2020-12-13 22:46

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 corres

2条回答
  •  难免孤独
    2020-12-13 22:53

    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)
    

    The result:

    enter image description here

    EDIT

    To center labels...

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

    Exporting as .png will cause problems...

提交回复
热议问题