I cant plot values above the tips of bars, XEndPoints dos not work
5 views (last 30 days)
Show older comments
I am using Matlab 2018b, I want to plot values above the tips of bars, where:
A=[4, 8 , 20 ,5];
B=[1,3 4 ,8 ];y
I want to write strings above the bars, but when I use XEndPoints, the following message is shown :
No appropriate method, property, or field 'XEndPoints' for class 'matlab.graphics.chart.primitive.Bar'.
my code:
function DisplayBarTopsisSawRankReversal(A,B,...
X1,X2)
max1= max(A(1,:));
max2=max(B(1,:));
MAX=max(max1,max2);
% This will produce 4 groups of 3 different colored bars
x = [1 2 ];
y = [A;B];
Bar = bar(x,y); % h will have 3 handles, one for each color of bars
set(Bar, {'DisplayName'}, {'Txt1','Txt2'}');
legend() ;
somenames={X1,X2};
set(gca,'xticklabel',somenames);
ylabel('Rank Reversal Ratio [%]')
ylim([0 MAX+30]);
%text(xtips1,ytips1,string( ) ,'HorizontalAlignment','center','VerticalAlignment','bottom')
opts = {'VerticalAlign','middle', 'HorizontalAlign','left', ...
'FontSize',8, 'Rotation',90};
text(1, A(1) ,'s' , opts{:});
numel(Bar)
xtips2 = b(2).XEndPoints;
end
How can I add strings in my case ?
1 Comment
Answers (1)
Star Strider
on 21 Dec 2020
If you want to put the numbers on the tops of the bars, this works:
x = [1 2 ];
y = [A;B];
figure
Bar = bar(x,y); % h will have 3 handles, one for each color of bars % Return ‘bar’ Handle
% xtips1 = b(1).XEndPoints;
% ytips1 = b(1).YEndPoints;
for k1 = 1:size(A,2)
ctr(k1,:) = bsxfun(@plus, Bar(k1).XData, Bar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = Bar(k1).YData; % Individual Bar Heights
end
for k1 = 1:size(ctr,2)
text(ctr(:,k1), ydt(:,k1), sprintfc('%d',ydt(:,k1)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
end
I have no idea what you are doing otherwise, or the result you want.
Make appropriate changes to put whatever you want at the tops of the bars.
4 Comments
Star Strider
on 21 Dec 2020
Rahim Rahim —
The easiest way is to use my code example to put what you want at the tops of the bars. (Use either the undocumented sprintfc or compose to create the appropriate strings.) I have no idea what you want to do with the rest of your code, since I do not understand what you are doing.
Wondimu Bantihun
on 20 Jun 2022
figure()
bargraph=[FND1,FND2,FND3,FND4,FND5;HND1,HND2,HND3,HND4,HND5;AND1,AND2,AND3,AND4,AND5];
X_as=categorical({'FND','HND','LND'});
bar(X_as,bargraph,'group');
how to put values on the top of the bar
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!