How to remove spaces between grouped bar chart

4.1k views Asked by At

Would somebody help me to remove spaces between groups of bars that appear when using plotting data in grouped bars. Here is the code

x = randn(1000,2);
[hy,hx] = hist(x);
bar(hx,hy,'barWidth',1)

This code generates this figure: enter image description here How can I change the code to remove the extra space between groups of bars.

What is interesting is that when plotting a single variable, the bars touch each other by using

bar(hx,hy(:,1),'barWidth',1)

enter image description here

So I wonder why the same should not work for multiple variables

1

There are 1 answers

0
zeeMonkeez On BEST ANSWER

You can plot the bar graphs separately, like so:

bar(hx, hy(:,1), 'barwidth', 1)
hold on
hb = bar(hx, hy(:,2), 'barwidth', 1);
set(hb, 'FaceColor', 'none', 'EdgeColor', [1, 0, 0])

Plotting the bars right next to each other without space would be ambiguous, as it wouldn't be clear which bars to group … But if this is really what you want to do:

xd=(hx(2)-hx(1))/2;
bar(hx, hy(:,1), 'barwidth', .5)
hold on
hb=bar(hx + xd, hy(:,2))
set(hb, 'FaceColor', 'none', 'EdgeColor', [.8, .3, .2], 'Barwidth', .5)