Effacer les filtres
Effacer les filtres

Same bins for histogram

62 vues (au cours des 30 derniers jours)
Kash Costello
Kash Costello le 7 Jan 2019
Commenté : Rik le 15 Juin 2020
Hi,
I have this code - attached in this post.
When you run this program, it would give you several subplots with histogram in some of them.
The thing is, I want to make the bins the same for all the histograms. I put "10" but the widths are still different.
I have no idea how to fix is, I am very confused. I also attached one image, just in case.
I want the widths to be the same for columns 2 and 3.
Thanks a lot!

Réponses (2)

Rik
Rik le 7 Jan 2019
What you are telling Matlab is to divide the range [min(data) max(data)] in 10 equal parts. Because your data has differing ranges, these sizes are different as well.
You can fix this by calculating appropriate bins yourself, and use those as an argument in the calls to histogram.
  3 commentaires
Benoit Espinola
Benoit Espinola le 15 Juin 2020
I do not think this comment solved the question. I believe it is a step forward however the solution is not evident. It arises yet another question: how do you calculate the bins yourself?
An example code would be great.
Rik
Rik le 15 Juin 2020
I believe it actually is evident once you take a look at the documentation. What exact bins are relevant will depend on your application. The documentation contains an example for how you can specify the bin edges. When you have decided what bin ranges are suited for your application you can use those same edges for every histogram. You may want to round min(data) and max(data) to determine those outer limits automatically, instead of hard-coding them as I did below.
% since the data in this example ranges from about -20 to 80:
edges=lispace(-20,80,20);
subplot(1,2,1)
histogram(data1,edges)
subplot(1,2,2)
histogram(data2,edges)

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 14 Jan 2019
Instead of passing in one number, that represents the number of bins, pass in an array for the 'edges' option. This is how you can specify the edges, or dividing lines, of the bins. You'll still have the same number of bins, but the location of the bins will not move around based on the data like they would if you passed just the number of bins.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by