MATLAB Answers

Bar width doesn't work when plotting two bar plots

2 views (last 30 days)
Lukas
Lukas on 27 Oct 2015
When I try to plot two similar but different distributions using bar over each other, setting 'BarWidth' to 1 does not seem to have the desired effect. For example, executing the code shown below, the second, red bar plot will still have spaces between it.
f1 = randn(10000,1);
f2 = f1;
f1s = sort(f1);
f2(end+1:end+500) = f1s((end-499):end)+rand(500,1);
[hf2, bf2] = hist(f2,20);
[hf1, bf1] = hist(f1,20);
b2 = bar(bf2,hf2);
hold on
b1 = bar(bf1,hf1);
set(b2,'FaceColor','g','BarWidth',1);
set(b1,'FaceColor','r','BarWidth',1.000001);
Anyone know why this is and how to solve it?
I've noticed that when I add a very small number to the 1 it will get rid of the space between the bars (as long as it is larger then the machine precision). So I've been using this as a work around, but this seems like a clunky work around.
Best

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!