Sarah Al-Jawhari
Sarah Al-Jawhari

Reputation: 133

how to display percentage label for each value on histogram in matlab

Using the answer to my previous question, I plotted the histogram for a cell array using:

   [nelements,centers]=hist(cellfun(@numel,S));
      numNeighbors = cellfun(@numel,S);
      [nelements,centers]=hist(numNeighbors,unique(numNeighbors))
      pcts = 100 * nelements / sum(nelements)
      figure
      bar(centers,pcts)

enter image description here

Displaying on the y axis the percentage of each xvalue occurrence, is it possible to show the percentages numbers on the histogram as I added in the image above so one can easily visualize the numbers?

Upvotes: 4

Views: 4305

Answers (2)

Ahmet Hayrullahoglu
Ahmet Hayrullahoglu

Reputation: 960

Check this out

Histogram Percentage

enter image description here

Upvotes: 0

craigim
craigim

Reputation: 3914

The text function is IMHO the friendliest of the annotation objects because it accepts graph coordinates rather than normalized figure coordinates.

K = numel(centers);
for k = 1:K
    text(centers(k),pcts(k),[num2str(pcts(k)) '%'],'HorizontalAlignment','center','VerticalAlignment','bottom')
end

This will put the percentage value at the top of each bar. Look at the help page for text for further enhancements, like controlling the position, color, font, etc. of the placed text.

Upvotes: 4

Related Questions