How can I plot a histogram with specified axis
7 次查看(过去 30 天)
显示 更早的评论
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 !
0 个评论
回答(1 个)
Guillaume
2015-12-17
编辑:Guillaume
2015-12-17
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!