# Y data on the bar plot

4 views (last 30 days)
Prasad Joshi on 19 Jan 2022
Commented: Prasad Joshi on 19 Jan 2022
I am using Bar plot for plotting the following code i am using
figure(1);
for i= 0:3
A= bar(ABC(:,i+1:i+3))
set(gca,'XTicklabel',{'25','50','75','100'})
xlabel('time')
for j=1:3
text(A(i),sprintf('(%.0f)',A(i)))
end
end
But I want the y data on it how can i proceed any hint i am using text command...Any changes in code can anyone suggest Thank you in advance ..Also i am using Matlab 2016b version

Ankit on 19 Jan 2022
text(A(i),sprintf('(%.0f)',A(i))) - your syntax seems doesn't right. Please follow the following syntax to defined values of y on your bar plot.
text(x,y,'string','PropertyName',PropertyValue....).
I tested this code on 2014b. Though it can be easily done with later version.
close all;
x = [25 50 75];
vals = [6000 3000 3100;7000 3100 3200;7900 3100 3200];
b = bar(x,vals);
dx = [-5, 0 , 5];
for i = 1:length(x)
for j = 1:length(vals)
text(x(i)+dx(j),vals(i,j),num2str(vals(i,j),'%.0f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
end
Prasad Joshi on 19 Jan 2022
Ankit can you please help me out in this question also it would be highly helpful and thank you for the links I will go through it .

### More Answers (2)

Simon Chan on 19 Jan 2022
Edited: Simon Chan on 19 Jan 2022
Why not using the example in the documentation by getting the EndPoints as follows:
clear;
clc;
x = [25 50 75];
vals = [6000 3000 3100;7000 3100 3200;7900 3100 3200];
b = bar(x,vals);
dx = [-5, 0 , 5];
text(cat(2,b.XEndPoints),cat(2,b.YEndPoints),string(cat(2,b.YData)),'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
##### 2 CommentsShowHide 1 older comment
Prasad Joshi on 19 Jan 2022
Thank you Simon chan ...You answer was highly helpful

KSSV on 19 Jan 2022
n = 5 ;
x = (1:n) ;
y = randi(n,n,1) ;
bar(x,y)
hold on
text(x,y+0.1,num2str(y))
ylim([0 6])
##### 1 CommentShowHide None
Prasad Joshi on 19 Jan 2022
Thank you KSSV