Why do the bars on my plot overlap when I create a BAR plot with only 2 bars?

5 views (last 30 days)
If I draw a BAR plot with 2 bars, and the distance between the bars is less than 0.8, MATLAB will draw them as overlapping. The following code will reproduce the problem:
bar([.25 .5],[2 3])

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This bug has been fixed for Release 14 (R14). For previous releases, please read below for any possible workarounds:
To work around the problem, you can hard code the bar width, using the following code as a guide:
barWidth = 0.2;
bar([.25 .5], [2 3], barWidth)
In MATLAB 6.5 (R13) code, the source of the problem is on lines 150 and 233 of $MATLAB/toolbox/matlab/specgraph/makebars.m, where $MATLAB denotes your root MATLAB directory. (This code may reside on different line numbers for previous releases of MATLAB.) In the case where there are 2 bars, these pieces of code return an empty logical array when they are expected to return either logical(0) or logical(1). One way to work around this problem is to replace each of the two sections of code (lines 147-151 and lines 233-244) with the following code:
if length(y)==1 | max(diff(y))==0,
equal = []; % Special case
else
equal = max(abs(diff(diff(y)))) <= max(max(abs(y)))*sqrt(eps);
if isempty(equal)
equal = 0;
end
end
PLEASE NOTE: This modification is not fully tested, and therefore cannot be guaranteed to work in all cases.
PLEASE NOTE: The file "makebars.m" is used by several graphics functions, including BAR, BAR3, BARH, BAR3H, and HIST. Therefore, changing the code puts these functions at risk of not working properly.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!