Reputation: 411
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:
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)
So I wonder why the same should not work for multiple variables
Upvotes: 3
Views: 4246
Reputation: 5177
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)
Upvotes: 3