如何删除分组条形图之间的空格

How to remove spaces between grouped bar chart

有人能帮我删除在分组柱中使用绘图数据时出现的柱组之间的 spaces 吗?这是代码

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

此代码生成此图: 我如何更改代码以删除条形图组之间的额外 space。

有趣的是,在绘制单个变量时,条形图通过使用

相互接触
bar(hx,hy(:,1),'barWidth',1)

所以我想知道为什么同样的方法不适用于多个变量

您可以单独绘制条形图,如下所示:

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

在没有 space 的情况下将条形图并排绘制会产生歧义,因为不清楚将哪些条形图分组……但如果这确实是您想要做的:

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)