Surstroemmingpirat
Surstroemmingpirat

Reputation: 73

Matlab histcounts show values on x-axis

I want to draw a histogram, using plot(histcounts(X,edges)). It works fine, except that on the x-axis, the number of the bin is displayed, not the actual value the bin refers to.

To make a bit clearer what I mean, I append two plots. Both display the same data, but for the first one, I used plot(histcounts(X,edges)) and for the second hist(X,edges). The plot for which I used hist shows the x-axis the way I want it to look like, with the value the bin refers to. I would like the plot(histcount(...) to have the same x-axis, instead of showing the bin number.

Histogram using plot(histcounts):

histogram using plot(histcounts)

Histogram using hist: histogram using hist

How can I change the x-axis to show this value instead of the bin number?

Thanks a lot!

Upvotes: 1

Views: 2701

Answers (1)

Vahe Tshitoyan
Vahe Tshitoyan

Reputation: 1439

If you have the edges, you can get the centres using

centres = edges(1:end-1)+ diff(edges)/2;

then the plot can be

plot(centres, histcounts(X,edges));

If you do not need to specify the edges you can get them using

[h_counts, edges] = histcounts(X);

Upvotes: 1

Related Questions