If you are plotting data y
using histogram(y)
, you can use the histogram
object it can return to do what you want;
h=histogram(y); hold on;
xvals = (h.BinEdges(2:end)+h.BinEdges(1:end-1))/2;
plot(xvals, h.Values, 'r');
The histogram object contains the height values (the bin counts) as well as the bin boundaries. Because there is one more bin boundary than there are bins, and because you would want to plot each point in the centre of the bin, take the average of the two nearest bin edge values (as I did in my calculation of xvals
).
To end this question, there is my final approach, which is basically the same as @Adriaan's answer.
Instead of working with index and mannual calculation, one can use the convolution method like this:
h=histgram(data); hold on;
plot(conv(h.BinEdges, [0.5,0.5],'valid'),h.BinCounts, 'Linewidth',2)
% h.BinCounts provides the data for y axies, while the previous is for x axies.
Document for the conv
function can be found here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.