Why do the x-tick labels overlap in the histogram produced by the HIST function within MATLAB after the axes' limits change?

6 views (last 30 days)
Why do the x-tick labels overlap in the histogram produced by the HIST function within MATLAB after the axes' limits change?
I am using the HIST function to produce a histogram of my data. If I specify the locations of the bin centers, then the x-tick labels sometimes overlap when I change the axes' limits. For example, if I execute the code:
x = [1 1 2 2 2 3 3 4 5 6 6 6 7 7 8 8 9];
figure;
hist(x,0:14);
axis([0 200 0 3])
The x-tick labels overlap each other. However, if I add one more bin center location, then the x-tick labels do not overlap after changing the axes' limits:
figure;
hist(x,0:15);
axis([0 200 0 3]);

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Mar 2019
The x-tick labels can overlap if the "XTickMode" property of the axes is set to "manual", and the axes limits are changed so the original x-tick locations are close together onscreen. The underlying cause of the problem is lines 72 and 73 of the BAR function, which is used to create the histogram:
if all(all(floor(x)==x)) & (size(x,1)<16),
set(cax,'xtick',x(:,1))
end
The problem occurs if both conditions are met, and "x" is used to set the "XTick" property values for the axes. Since the "XTick" property values are being set, the "XTickMode" property will become "manual". Later, if the axes' limits are changed, the, the "XTick" property locations will not be recalculated, because the "XTickMode" property is set to "manual". Therefore, the same values will be used for the x-tick locations. If both conditions are not met, then the "XTick" property will not be set, and the "XTickMode" property will remain set to "auto". If the axes' limits are changed, then the locations for the "XTick" locations will be recalculated.
You could avoid the problem by:
1) Changing the number that the row-size of "x" is being compared to in the BAR function, from 16 to a smaller number. Change line 72 to something like:
if all(all(floor(x)==x)) & (size(x,1)<10),
Since neither of the conditions will be met, the "XTick" property will not be set, and the "XTickMode" will not become set to "manual".
2) Setting the axes' "XTickMode" to "auto" in the HIST function, after returning from BAR. Change line 77 in HIST to:
h = bar(x,nn,'hist');
and then add these lines:
ax = get(h,'Parent');
set(ax,'XTickMode','auto');
This way, the "XTick" property values will be recalculated if the axes' limits are changed.
You can find more information about setting the "XTick" and "XTickMode" properties at the following URL:

More Answers (0)

Tags

Products


Release

R13SP1

Community Treasure Hunt

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

Start Hunting!