How Can I fix the width of Bins?
5 views (last 30 days)
Show older comments
Hi Every body, I would like to tell you that I am beginner in Matlab. So please I need your help.
I want to fix the width of bins of the histogram, My code is:
% cd F:\Script\Center_Marsh\Center_Marsh_1992
H= Dec_1992(:);
hist(H);
subplot(1,2,1)
hist(H)
xlabel('NDVI','fontsize',14)
ylabel('AVHRR Pixels','fontsize',14)
set(gca,'XMinorTick','on')
set(gca,'YMinorTick','on')
set(gca,'XGrid','on')
set(gca,'YGrid','on')
set(gca,'GridLineStyle',':','linewidth',1)
set(gca,'TickDir','out')
set(gca,'TickLength',[.015 .025])
axis([0.0 0.5 0 110])
save('Dec_Hist_1992.fig');
D= Dec_1992(:);
[hi,cx]=hist(D);
subplot(1,2,2), plot(cx, hi,'k', 'LineWidth', 3)
xlabel('NDVI','fontsize',14)
ylabel('AVHRR Pixels','fontsize',14)
set(gca,'XMinorTick','on')
set(gca,'YMinorTick','on')
set(gca,'XGrid','on')
set(gca,'YGrid','on')
set(gca,'GridLineStyle',':','linewidth',1)
set(gca,'TickDir','out')
set(gca,'TickLength',[.015 .025])
axis([0.0 0.5 0 110])
save('Dec_Hist_1992.fig');
Thank you
Reyadh
4 Comments
Walter Roberson
on 30 May 2015
If you fix the number of bins and you also fix the bin width, then it might not be possible to fit all of the data. Even in cases where it is possible, you have ambiguity.
Imagine that the requested bin width is 2 and imagine that there is 1 point to be covered. Possible coverings include
|x |
| x |
| x|
The bin width was fixed, the number of bins was fixed, but the policy was not fixed.
Why does it matter? Because of fairness. Consider 2 bins of fixed width, and 4 points.
| xx x| x | -> centered but uneven distribution
| xx |x x | -> not centered but equal distribution
| x|x x x| -> not centered, unequal
There are techniques to dynamically move the boundaries to get the most even distribution (defined by lowest sum of squares of number of elements in each bin), but for a simple histogram you place your boundaries first and then you count once, not knowing ahead of time how the points distribute. So basically you start with the information about where the first point is and where the last point is; and you have forced the number of bins and the bin widths; and now you need a policy about where to put the bin centers to distribute the mod((max-min),bin_width) extra space.
Answers (1)
Star Strider
on 30 May 2015
I would use hist to calculate the histogram information, then use bar to do the plot, since it will let you vary the width of the bars.
2 Comments
Star Strider
on 30 May 2015
My pleasure.
I actually thought you intended bar width, since it is easy to set the bins in the hist function or its friends.
To get the histogram data to use with the bar plot, request it as an output from hist. See the documentation for Use hist to Calculate Only. (I see that in your code, but the documentation shows how to use it with bar.)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!