Plot occurrences of values with bin edges, by using "histcounts" and "plot" functions

2 views (last 30 days)
Sim on 8 Mar 2022
Edited: Sim on 8 Mar 2022
Hi, I have 1000 normally distributed random numbers (with mean = 3 and standard deviation = 10):
X = normrnd(3,10,[1,1000])
I want to partition the X values into 100 bins, and return the count in each bin (i.e. the occurrence of values within each bin), as well as the bin edges:
[n,bin_edges] = histcounts(X,100);
Then, I want to plot the count in each bin together with the values of the bin edges.... Should I (maybe) use the average value of two edges, i.e. the, or is there any other way to plot the count of my values with the bin edges?
plot(bin_edges(1:100),n); % this is not correct, because I plot the left edge for each occurrence of values

Accepted Answer

Steven Lord
Steven Lord on 8 Mar 2022
Why not just use histogram?
X = normrnd(3,10,[1,1000]);
h = histogram(X, 100);
c = h.BinCounts;
e = h.BinEdges;
for whichBin = 1:5
fprintf("In bin %d [%g, %g) there were %d elements.\n", ...
whichBin, e(whichBin:whichBin+1), c(whichBin));
In bin 1 [-26, -25.417) there were 1 elements. In bin 2 [-25.417, -24.834) there were 2 elements. In bin 3 [-24.834, -24.251) there were 1 elements. In bin 4 [-24.251, -23.668) there were 0 elements. In bin 5 [-23.668, -23.085) there were 2 elements.
  1 Comment
Sim on 8 Mar 2022
Thanks a lot @Steven Lord! Yes, right, the histogram function plots the count of values in each bin and we can extract both BinCounts and BinEdges, but, still, I would like to use the plot function to show markers and lines for the "skyline" of the histogram....... In general, I would like to avoid the typical histogram representation....... :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!