Errorbars on bar-graph

后端 未结 2 1249
终归单人心
终归单人心 2021-01-06 19:00

I\'m trying to get: a nice bar graph of groups. I\'d like error bars on each bar like this (or similar at your discretion):

相关标签:
2条回答
  • 2021-01-06 19:19

    It's really not that crazy!

    You can just use errorbar with the data that you have readily available.

    This code computes the correct locations, by adding 1 for every group and 1/7 for each bar within a group.

    for i = 1:5
        j = 1:6; 
        x = -0.5 + i + 1/7 * j; 
        errorbar(x, ff(j,i), ee(j,i), '.'); 
    end
    

    Results in:

    enter image description here

    (Leaving out the labels, but otherwise this seems pretty similar to what you were looking for)

    0 讨论(0)
  • 2021-01-06 19:37

    the centre of each bar can be obtained by

    x = get(get(h(i),'children'),'xdata');
    barsx=mean(x,1);
    

    barsx gives the center for every ith element of every bar subset.

    h=bar(bars)
    for i=1:6
      x = get(get(h(i),'children'),'xdata');
      barsx(1:6,i)=mean(x,1)
    end
    hold all
    h=errorbar(barsx,bars,barsvar)
    

    to have the errors having the same colors as bar:

    figure()
    h=bar(bars) 
    col=[0 0 1;0 1 0;1 1 0; 1 1 1; 0 0 0; 0 1 1];
    colormap([0 0 1;0 1 0;1 1 0; 1 1 1; 0 0 0; 0 1 1])
    hold all
    for i=1:6
      x = get(get(h(i),'children'),'xdata')
      barsx=mean(x,1)
      h1=errorbar(barsx',bars(1:6,i),barsvar(1:6,i),'color',col(i,:))
      set(h1,'linestyle','none')
    end
    
    0 讨论(0)
提交回复
热议问题