How to add text in a bar in a multiple bar plot?
87 views (last 30 days)
Show older comments
German Preciat Gonzalez
on 20 Dec 2016
Commented: ammara khurshid
on 1 Oct 2017
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 Comments
Star Strider
on 20 Dec 2016
It would help to know the MATLAB version you’re using. The solution is different for R2014b and later releases than for prior versions.
Accepted Answer
José-Luis
on 20 Dec 2016
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 Comments
ammara khurshid
on 1 Oct 2017
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.
More Answers (1)
Brendan Hamm
on 20 Dec 2016
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
0 Comments
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!