How do I label the bars in my histogram?

150 views (last 30 days)
How do I label the bars in my histogram?
I would like to label the bars of my histogram plot that I created using HIST. I would like to add the count to the top of the bar.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Jun 2017
As of MATLAB R2017a, there is no built-in feature to add bin count labels to histogram plots. Although this is not a built-in feature of MATLAB, you may label the bars of your histogram by using the output of the HIST command to create text objects. The following is an example that you can use as a guide:
% Create random data for the sake of the example
data = 10*rand(1,100);
% Draw the histogram
% 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
Arno Vaik
Arno Vaik on 3 Mar 2019
I also followed the same instructions (r2018b) but received this error message:
>> text(x,n,barstrings,'horizontalalignment','center','verticalalignment','bottom')
Error using text
First two or three arguments must be numeric doubles.
How could I handle that?

Sign in to comment.

More Answers (1)

Mohamed Nedal
Mohamed Nedal on 27 May 2017
Hello, I tried it but I got this error:
Error using text
Value must be a column or row vector
Error in Hist_long_3groups (line 29)
How do I solve it?
  1 Comment
Mukul Rao
Mukul Rao on 19 Jun 2017
I tried this code snippet and it worked fine for me. Please provide information on your MATLAB version and if you modified the code snippet described in the original post.
- Mukul

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!