How can I plot the percentage value in an histc plot?

3 ビュー (過去 30 日間)
Henri Giudici
Henri Giudici 2015 年 6 月 25 日
回答済み: Mukul Rao 2015 年 6 月 26 日
Hi, I have an histogram. In x-axis I have the name of each bar and in y-axis the data quantity. I would like to plot up to each bar of the histogram the respectively percentage. How can I do that?

回答 (1 件)

Mukul Rao
Mukul Rao 2015 年 6 月 26 日
Hi , I assume by "up to" you mean on top of the histogram. I recommend using text objects and adding them as shown in the code below:
%Generate data, use example from histc documentation
rng(0,'twister')
x = randn(100,1);
binranges = -4:4;
[bincounts] = histc(x,binranges);
%Compute percent contributions
percentagevalues = bincounts./sum(bincounts) * 100;
%Create a histogram
figure
bar(binranges,bincounts,'histc')
for i = 1:length(bincounts)-1
%Determine x location of label
x = (binranges(i) + binranges(i+1))/2;
%Determine y location of label, offset it by some distance from the
%top of the bar
y = bincounts(i)+1;
%Create a text object
text(x,y,num2str(bincounts(i),'%3.2f'),'HorizontalAlignment','center');
end
Note if this example doesn't address your question, please provide additional information so that some other person from the community can provide an alternate solution.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by