How do I label the bars in my histogram with the function "histogram" ?

27 ビュー (過去 30 日間)
Sim
Sim 2022 年 7 月 1 日
コメント済み: Voss 2022 年 7 月 3 日
In a similar thread a user asked: How do I label the bars in my histogram?
However, that person mentioned the function hist, instead of the function histogram, and the solution proposed by @MathWorks Support Team was therefore based on the function hist:
% Create random data for the sake of the example
data = 10*rand(1,100);
% Draw the histogram
hist(data);
% Get information about the same
% histogram by returning arguments
[n,x] = hist(data);
% Create strings for each bar count
barstrings = num2str(n');
% Create text objects at each location
text(x,n,barstrings,'horizontalalignment','center','verticalalignment','bottom')
Now, how do I label my bars but using the function histogram instead of hist ?
Indeed, the function histogram has not "counts" and "centers" as the function hist:
[counts,centers] = hist(___)

採用された回答

Voss
Voss 2022 年 7 月 1 日
編集済み: Voss 2022 年 7 月 1 日
% Create random data for the sake of the example
data = 10*rand(1,100);
% Draw the histogram and return histogram object
h = histogram(data);
% Get information about the histogram
edges = get(h,'BinEdges');
n = get(h,'Values');
% Create strings for each bar count
barstrings = num2str(n');
% Create text objects at each location
x = (edges(1:end-1)+edges(2:end))/2;
text(x,n,barstrings,'horizontalalignment','center','verticalalignment','bottom')
  3 件のコメント
Sim
Sim 2022 年 7 月 3 日
Thanks a lot @Voss!! Cool answer!! :-)
Voss
Voss 2022 年 7 月 3 日
Thank you, and you're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by