# bar plot, value on top

806 views (last 30 days)
gabe on 8 Jun 2012
Hi This actually works to some extent but the problem is i got all the values for each bar shown on top. So for each bar all the Y values are being shown.
x=[1:2:23]';
y=abs([121 41 20.6 12.5 8.1 5.8 4.4 3.5 3 2.7 2.3 2.1]);
bar(x,y)
text(x,y,num2str(y,'%0.2f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
Please tell me what is wrong! Thanks!
##### 2 CommentsShowHide 1 older comment
Elimelech Schreiber on 9 Dec 2020
num2str(y,'%0.2f')
converts the numbers in y, into one long string, and therefor it's repeated above each bar.
you need to have a seperate string for each value. The easiest fix here woul be:
num2str(y','%0.2f')

Andrei Bobrov on 8 Jun 2012
x=[1:2:23]';
y=abs([121 41 20.6 12.5 8.1 5.8 4.4 3.5 3 2.7 2.3 2.1]);
bar(x,y)
for i1=1:numel(y)
text(x(i1),y(i1),num2str(y(i1),'%0.2f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
Javier Gonzalez-Rocha on 22 Aug 2017
Try generating an arbitrary x array of length equal to y. Omit the x array in your plot using the gca handle as shown below:
Y=[198 138 172 188 190 192];
% arbitrary array
X = 1:6;
figure
bar(X,Y);
labels = arrayfun(@(value) num2str(value,'%2.2f'),Y,'UniformOutput',false);
text(X,Y,labels,'HorizontalAlignment','center','VerticalAlignment','bottom')
% clears X axis data
set(gca,'XTick',[]);

### More Answers (5)

Elimelech Schreiber on 5 Nov 2017
I've written a function to do just this. It's called barvalues, and is very easy yo use.
simply:
bar(x);
barvalues;
Elimelech Schreiber thank you very much. This function is very useful. Congratulations.

Ross on 21 Mar 2017
Can be a little cleaner than the above solutions, by making a cell array with the labels in advance using the arrayfun function.
x=[1:2:23]';
y=abs([121 41 20.6 12.5 8.1 5.8 4.4 3.5 3 2.7 2.3 2.1]);
bar(x,y)
labels = arrayfun(@(value) num2str(value,'%2.1f'),y,'UniformOutput',false);
text(x,y,labels,...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')

Junaid Qadir on 20 Dec 2018
@ Andrei Bobrov I really appreciate your experience. Thank You so much you solved my problem. Keep help with the needy peoples. Thanks

Tony Castillo on 18 Jan 2019
Hi guys,
But despite I have tried to put the avlues on the TOP/OVER every one bar, I could not, even in the code I already change botton for cap, but it has been unsuccesfull.
Could you help me to set the values on the peak of every one bar?.
The code is attached.
Thanks

Junaid Qadir on 18 Jan 2019
Edited: Junaid Qadir on 18 Jan 2019
Please Try this code, wish your problem will be solve. Best of luck
clc
clear all
close all
x = [1 2 3 4 5 6 7 8 9 10];
y = [200 250 300 350 400 450 500 550 600 700 ];
z= bar(x,y)
for i1=1:numel(y)
text(x(i1),y(i1),num2str(y(i1),'%0.0f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
% text(1,43,'42','Color','k','FontSize',9)
% plot(x,y, '->','LineWidth',2)
xlabel('Friends')
ylabel(' Record')
title('Friends List')
grid on
ax=gca;
ax.XTick = [1 2 3 4 5 6 7 8 9 10 ];
ax.XTickLabels = {'Junaid', ' Qadir' ,' Sahar ',' Ubaid ',' Munsif ',' Yousaf ',' Sami ','Najm',' Shakir', ' Pakistan'};
ax.XTickLabelRotation = 45;
z.FaceColor = 'flat';
% z.EdgeColor= '[1,0,0]';
z.CData(1,:) = [1,1,0];
z.CData(2,:) = [1,1,0];
z.CData(3,:) = [1,1,0];
z.CData(4,:) = [1,1,0];
z.CData(5,:) = [1,1,0];
z.CData(6,:) = [1,1,0];
z.CData(7,:) = [1,1,0];
z.CData(8,:) = [1,1,0];
z.CData(9,:) = [1,1,0];
z.CData(10,:) = [1,1,0];
##### 1 CommentShowHide None
A. H. Choudhury on 24 Oct 2020
Dear bro, kindly give a code for the same process for a grouped bar diagram