How to Plot Numbers on top of Bar graphs?

1,012 views (last 30 days)
Assume we have this as data
Y=[198 138 172 188 190 192];
How would I go about plotting the Y-values of each of the bars on top of the bars? So each bar would show its value on top of it.

Accepted Answer

Chad Greene
Chad Greene on 7 Aug 2017
Edited: Chad Greene on 7 Aug 2017
Use the text function like this:
Y=[198 138 172 188 190 192];
box off
Chad Greene
Chad Greene on 20 Nov 2017
The phrase "it isn't working" does not contain enough information to help. I suggest reading this, then start another question with specifics about the issue you're having.

Sign in to comment.

More Answers (3)

Johannes Stoerkle
Johannes Stoerkle on 21 Feb 2020
Edited: Johannes Stoerkle on 21 Feb 2020
As an extension, I found a good approach if one wants to plot the number on the top of bars, which are displayed in several groups. Therefore, I estimate the correction for the text x-Position using an exp-function, which is fitted to my empirical results.
For instance. If you consider 3 categories and 7 different models (determined in first line) it results:
dataSample = reshape([36 2:21],3,7)';
catStrArray = {'category1','category2','category3'};
catArray = categorical(catStrArray);
catArray = reordercats(catArray,catStrArray);
nModel = size(dataSample,1);
nCat = size(dataSample,2);
xPosAmpl = 0.3682626-0.3298725*exp(-0.407004*(nModel-1)); % position amplitude
xPosInc = 2*xPosAmpl/(nModel-1);
modelNames = [];
for idxModel=1:nModel
bar_xPos = 1:nCat;
if nModel~=1
bar_xPos = bar_xPos-xPosAmpl+(idxModel-1)*xPosInc;

Soyeun Jung
Soyeun Jung on 7 Aug 2017
Hi Ibro, you can run this line of code to display the y-values on top of each bar. See the attached link for related information.
text([1:length(Y)], Y', num2str(Y','%0.2f'),'HorizontalAlignment','center','VerticalAlignment','bottom')

Edgar Mauricio Ocampo Alvarez
Edited: Edgar Mauricio Ocampo Alvarez on 23 Apr 2022
% this is the best solution:
xtips1 = bar(1).XEndPoints; ytips1 = bar(1).YEndPoints;
text(xtips1, ytips1, num2str(Y1,'%0.2f'),'HorizontalAlignment','left','VerticalAlignment','middle','Rotation',90,'FontSize',8);
xtips2 = bar(2).XEndPoints; ytips2 = bar(2).YEndPoints;
text(xtips2, ytips2, num2str(Y2,'%0.2f'),'HorizontalAlignment','left','VerticalAlignment','middle','Rotation',90,'FontSize',8);

Community Treasure Hunt

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

Start Hunting!