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

조회 수: 382 (최근 30일)
ammara khurshid
ammara khurshid 2017년 10월 1일
댓글: dpb 2024년 1월 19일
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
Mohammed Abdallatif 2019년 11월 6일
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일
편집: 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