How can I plot a histogram with specified axis

7 vues (au cours des 30 derniers jours)
stanley
stanley le 17 Déc 2015
Modifié(e) : Guillaume le 17 Déc 2015
I have a row of data. For example [0 0.1 0.2 0.3 0.1 0.2]
I want to plot a histogram that have same number of bins with the number of data.
My final goal is that the histogram have 5 bins and 1st bin has value 0 2nd bin has value 0.1 and so on.
Thank you very much !

Réponses (1)

Guillaume
Guillaume le 17 Déc 2015
Modifié(e) : Guillaume le 17 Déc 2015
Use unique to get the sorted unique values of your array and use that as your bins. Note that due to the way histcounts / histogram work with the last bin you need to add an extra bin at the end ( +Inf works):
data = [0 0.1 0.2 0.3 0.1 0.2]
bins = [unique(data) Inf];
h = histcounts(data, bins)
Important: If your data has been generated by some mathematical calculations then there may be some very small differences between your first and second 0.1 that matlab by default does not show. However, unique won't consider them equal. If that is the case, use uniquetol instead. To see what I mean, compare:
>>data = [0.1+0.1+0.1 0.3]
data =
0.3 0.3
>>unique(data)
ans =
0.3 0.3
>>uniquetol(data)
ans =
0.3

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by