How do i label each bar in bar group with a "string" on top?

227 次查看(过去 30 天)
Hi ! Urgent help needed. I have a grouped bar graph and i want to add text on the top of each bar in each group. I am doing this way:
y = [58.1395 62.7907; 40.3900 40.3400]
Y=bar(y)
str={'John'; 'Adam'};
set(gca, 'XTickLabel',str, 'XTick',1:numel(str))
labels= {'Physics', 'Chemistry'; 'Physics', 'Chemistry'};
xt = get(gca, 'XTick');
text(xt, y, labels, 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
Am getting error "error using text Value must be a column or row vector in bar plot" and not able to fix it. Am working in R2015b. Help me.

采纳的回答

dpb
dpb 2017-10-1
编辑:dpb 2019-12-12
...
hB=bar(y); % use a meaningful variable for a handle array...
hAx=gca; % get a variable for the current axes handle
hAx.XTickLabel=str; % label the ticks
hT=[]; % placeholder for text object handles
for i=1:length(hB) % iterate over number of bar objects
hT=[hT text(hB(i).XData+hB(i).XOffset,hB(i).YData,num2str(hB(i).YData.','%.3f'), ...
'VerticalAlignment','bottom','horizontalalign','center')];
end
The text command does the two groups with the two bars of each group labeled in the one call for each bar group. The x position is that of the data plus the offset and the y position is the data value. The label is formatted to string to be written by num2str; note carefully the transpose operator .' to create a column vector; this is imperative or the two values would be strung together on a single line.
The "trick" is in the hB.XOffset term; the 'XOffset' property is hidden so have to know it exists a priori as there's nothing to give away its existence nor any other data available from which to compute the dX that the routine uses internally for the offset of the bar center from the x location of the group. As noted above, this is a real foopah on TMW's part...
The above results in
ADDENDUM
Specifically addressing the concern that didn't do the labels,
hT=[]; % placeholder for text object handles
for i=1:length(hB) % iterate over number of bar objects
hT=[hT,text(hB(i).XData+hB(i).XOffset,hB(i).YData,labels(:,i), ...
'VerticalAlignment','bottom','horizontalalign','center')];
end
results in
instead...
  9 个评论
Benjamin Kraus
Benjamin Kraus 2024-1-19
编辑:Benjamin Kraus 2024-1-19
@dpb we hear and appreciate your feedback. There is a reason I've been looking at these particular MATLAB Answers posts recently. Keep your eyes open for a future release, and keep the feature requests coming.
dpb
dpb 2024-1-19
That will be much anticipated, indeed...the only bad part will be that to keep backwards compatibility, probably the only way to fix things will be to introduce a whole new replacement function instead of being able to add onto or replace in situ the existing one; thereby adding to the code bloat issue.
I don't have a solution for that, unfortunately, other than perhaps an introduction of a compatibility flag that would let users choose to orphan functions prior to a given release, but the namespace collisions otherwise probably make such unworkable.

请先登录,再进行评论。

更多回答(3 个)

Ben Oeveren
Ben Oeveren 2018-9-12
编辑:dpb 2018-9-12
For those still searching for a solution. Based on previous comments I've used this:
y = grpstats(x,grps,'sum');
ytxt = round(y./repmat(nansum(y,2),1,size(y,2))*100);
ytxt = num2cell(ytxt);
ytxt = cellfun(@(x) [sprintf('%0.1f',x), '%'], ytxt,'un',0);
figure('Position',[107 516 813 182]);
hB = bar(y,'group','EdgeColor','w','BarWidth',1);
hT=[]; % placeholder for text object handles
for i=1:length(hB) % iterate over number of bar objects
hT=[hT,text(hB(i).XData+hB(i).XOffset,hB(i).YData, ytxt(:,i), ...
'VerticalAlignment','bottom','horizontalalign','center')];
end
grid on
  1 个评论
dpb
dpb 2018-9-12
编辑:dpb 2018-9-12
NB: Can write the code to produce the strings from the numeric values a little more simply -- don't need to cast to cell array first:
ytxt=cellstr(num2str(ytxt(:),'%0.1f%%'));

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-10-1
You can use text() to put text atop each bar. Attached is a demo/example.
  4 个评论
dpb
dpb 2017-10-2
编辑:dpb 2017-10-2
It's not text that's the problem, it's that the x/y position of the data is not where the bar's position is for grouped bar...and TMW doesn't make it easy to find where that position is by hiding the necessary property. :(
And, to be clear, the comment wasn't aimed at you but intended to again highlight the absurdity of the hidden property for what is an obvious need and why there isn't a feature implemented at a much higher level to allow for such simple tasks...Matlab is, after all, supposed to be a "high level" language for rapid development--why is it so much needed to do so much at such a low level to make decent graphics still after some 30 years????
dpb
dpb 2017-10-2
编辑:dpb 2017-10-3
And to amplify further, im(nsh)o < :) >, bar series should have builtin property of label for each bar similar to an axes object 'X|Y|ZTickLabel' with associated properties for positioning it in|outside the bar, horizontal|vertical, etc., etc., etc., ... If just boggles the mind the number of hours folks have spent cumulatively over the years dealing with such trivia instead of actually solving the underlying problem they're working on.

请先登录,再进行评论。


Mohammed Abdallatif
Can someone help to locate the Description (Labels) in the middle of each section of the bar instead of the top? I must change the Y-coordinate of the text, but it needs to be automatically centered and I don't know how.
Code:
text(b(i).XData(n), b(i).get.YEndPoints(n),Labels(n,counter),'HorizontalAlignment','center',...
'VerticalAlignment','bottom');
Unbenannt.png
  2 个评论
dpb
dpb 2019-11-6
You have the positions of each bar section; just compute the means of each section. The first section has an implied lower limit of 0; the bottom of each of the others is the top of the preceding.
Mohammed Abdallatif
编辑:Mohammed Abdallatif 2019-11-7
thanks @dpb for the idea.
I solved the current problem now using the moving mean function by adding 0 to Origin Ydata:
MovingMeanOfYdata = movmean([0 OriginYdata],[1 0]);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Bar Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by