Putting space between bars using bar function

1.5k Views Asked by At

I am try to plot 16 bars, 8 of these belong to one group. I want to give this group the red color, the other 8 belong to another group which is given the blue color. I would like to arrange the bar in pairs, each pair containing one from the red group and one from the green group. I have tried the following:

bar(num1,info(1:2:end);
bar(num2, info(2:2:end);

in which info contains 16 values I want to plot, num1=1:2:numel(info) and num2=2:2:numel(info). If I do it this way, all the bars are placed adjacent to their neighbors and there are no gaps between bars. Ideally, to improve visualization, there should be space in between pairs but no space within a pair. For instance, bar1bar2 <space> bar3bar4 <space> bar5bar6<space>...

Could someone help me with this spacing issue? Thank you so much!

1

There are 1 best solutions below

2
On BEST ANSWER

See the docs for bar. You can reshape info to 2xN (instead of its current shape 1x2N) and then use a single bar command to plot the 2 series, and it will take care of the spacing.

See this image from the docs: enter image description here

If you want to keep doing it your way, you could just tweak num1 and num2:

N = numel(info) / 2;
num1 = (1:N) * 3;
num2 = (1:N) * 3 + 1;