Most efficient way of drawing grouped boxplot matlab

前端 未结 2 1721
刺人心
刺人心 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 23:02

    I had the same problem with grouping data in a box plot. A further constraint of mine was that different groups have different amounts of data points. Based on a tutorial I found, this seems to be a nice solution I wanted to share with you:

    x = [1,2,3,4,5,1,2,3,4,6];
    group = [1,1,2,2,2,3,3,3,4,4];
    positions = [1 1.25 2 2.25];
    boxplot(x,group, 'positions', positions);
    
    set(gca,'xtick',[mean(positions(1:2)) mean(positions(3:4)) ])
    set(gca,'xticklabel',{'Direct care','Housekeeping'})
    
    color = ['c', 'y', 'c', 'y'];
    h = findobj(gca,'Tag','Box');
    for j=1:length(h)
       patch(get(h(j),'XData'),get(h(j),'YData'),color(j),'FaceAlpha',.5);
    end
    
    c = get(gca, 'Children');
    
    hleg1 = legend(c(1:2), 'Feature1', 'Feature2' );
    

    colored grouped boxplot with varying group sizes

    Here is a link to the tutorial.

提交回复
热议问题