How to add text in a bar in a multiple bar plot?

조회 수: 173(최근 30일)
German Preciat Gonzalez
German Preciat Gonzalez 2016년 12월 20일
댓글: ammara khurshid 2017년 10월 1일
Hi all,
I have a matrix M like this:
M = [99.0000 93.0000 65.0000 95.0000 97.0000 97.0000;
84.0000 95.0000 79.0000 83.0000 92.0000 92.0000;
91.0000 89.0000 74.0000 93.0000 75.0000 81.0000;
93.0000 88.0000 93.0000 97.0000 89.0000 91.0000;
76.0000 80.0000 71.0000 80.0000 71.0000 80.0000;
80.0000 70.0000 35.0000 53.0000 71.0000 73.0000;
92.5197 91.7485 73.3855 90.6445 90.2386 91.9565]
and when I plot it I obtain this
hb = bar(percentageMatrixEC);
I would like to add on each bar a number, this numbers:
numbersToAdd = [162 162 162 148 150 156;
101 101 101 96 86 75;
75 75 75 72 70 71;
58 58 58 58 53 56;
21 21 21 20 17 20;
20 20 20 19 17 15;
512 512 512 482 462 461]
I would like something like this:
I was looking in MATLAB help but I couldn't find anything for bars with multiple groups.
If you can help me I will really appreciate it!
Cheers!
  댓글 수: 4
Star Strider
Star Strider 2016년 12월 20일
It would help to know the MATLAB version you’re using. The solution is different for R2014b and later releases than for prior versions.

댓글을 달려면 로그인하십시오.

채택된 답변

José-Luis
José-Luis 2016년 12월 20일
M = [99.0000 93.0000 65.0000 95.0000 97.0000 97.0000;
84.0000 95.0000 79.0000 83.0000 92.0000 92.0000;
91.0000 89.0000 74.0000 93.0000 75.0000 81.0000;
93.0000 88.0000 93.0000 97.0000 89.0000 91.0000;
76.0000 80.0000 71.0000 80.0000 71.0000 80.0000;
80.0000 70.0000 35.0000 53.0000 71.0000 73.0000;
92.5197 91.7485 73.3855 90.6445 90.2386 91.9565];
hb = bar(M);
numbersToAdd = [162 162 162 148 150 156;
101 101 101 96 86 75;
75 75 75 72 70 71;
58 58 58 58 53 56;
21 21 21 20 17 20;
20 20 20 19 17 15;
512 512 512 482 462 461];
barWidth = hb.BarWidth;
numCol = size(M,2);
cnt = 0;
for ii = numbersToAdd'
cnt = cnt + 1;
xPos = linspace(cnt - barWidth/2, cnt + barWidth / 2, numCol+1);
idx = 1;
for jj = xPos(1:end-1)
val = numbersToAdd(cnt,idx);
y = M(cnt,idx);
text(jj, y + 1, num2str(val));
idx = idx +1;
end
end
  댓글 수: 2
ammara khurshid
ammara khurshid 2017년 10월 1일
Hello! i was in search of the same concept and this helped me. i need a bit change in this code"i want to write text like 'A', 'B', 'c' etc. at the top of the each bar of each group. help kindly.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Brendan Hamm
Brendan Hamm 2016년 12월 20일
This requires us to find information about the location of each of the individual bars and calculate the centers for the labels. This can be done with the bar property BarWidth. I found it easiest to find the left edge of the set of bars and add an offset at each iteration. I wrote a helper function for the offset and text creation and call it in a for loop.
hb = bar(M);
% Make Left bars first in list so that the kth bar is made up of the kth
% column of data.
hb = flipud(hb);
% Number of XAxisValues:
numXVals = numel(hb);
% Find the width of a bar
dx = hb(1).BarWidth/numXVals;
% Find the Left Most Edge of each X Values bars
leftEdge = hb(1).XData - hb(1).BarWidth/2;
% Adjust to center of left bar
leftEdge = leftEdge + dx/2;
for k = 1:numXVals
labelBar(hb(k),dx,leftEdge,k,numbersToAdd(:,k))
end
function labelBar(hb,dx,leftEdge,k,lbl)
% YData Location
yLoc = hb.YData;
% Adjust offset from left edge of bars
xLoc = leftEdge + (k-1)*dx; % Find position from Left Edge
txt = num2str(lbl);
text(xLoc,yLoc,txt,...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom');
end

Community Treasure Hunt

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

Start Hunting!

Translated by