# Data labels above bars on grouped bar plot

191 views (last 30 days)

Show older comments

I'm making a grouped bar plot (i.e., a bar plot with multiple bars in each category). I would like to add labels at the top of each bar to indicate its height. I was planning to just use the text function. However, bars within a given group all have the same x location (i.e get(h,'XData') is the same for all bar series). I'm not sure how to find the proper x location for each bar within a given group. Any ideas?

Thanks, Justin

##### 2 Comments

### Accepted Answer

dpb
on 16 May 2014

Edited: dpb
on 16 May 2014

You're on the right track, the center of each group is at the axis tick value. For each bar in the group, use that value plus/minus a delta to locate the x position for your text.

I did an example of this for another poster within the last few weeks at most altho I don't have the link at hand. Perhaps a search will uncover it.

ADDENDUM:

OK, I looked at past answers--this one is pretty close altho I thought I did another. Maybe it was in the newsgroup instead...

##### 6 Comments

dpb
on 3 Oct 2016

Just past 'em together...

Y=[Y1;Y2;Y3].'; % create nx3 column array for *bar*

NB: The latest release of bar uses HG2 and returns a handle to a barobject, rather than the former bar series objects. This new object is essentially opaque to the the details and the data for the patches used to draw the bars isn't available from which to compute the individual bar positions so labelling bars as this example does won't work. I've not got a more recent version so not sure what the workaround is, if there is one or if one must resort back to the earlier "trick" I illustrated before.

### More Answers (5)

Will Adler
on 18 Nov 2014

##### 8 Comments

Kelly Kearney
on 21 Sep 2016

Moving the labels inside the bars is a simple matter of changing the horizontal alignment so the right edge, rather than left, aligns with the bar height (and in most cases, adding a small offset to the y-position so the text doesn't sit flush to the bar edge).

This example also assumes that your bars are all large enough to fit the full text string.

Y=random('unif',30,100,[4 3]); % sample data

h=bar(Y);

yb = cat(1, h.YData);

xb = bsxfun(@plus, h(1).XData, [h.XOffset]');

hold on;

padval = 1;

htxt = text(xb(:),yb(:)-padval, cellstr(num2str(yb(:))), ...

'rotation', 90, 'horiz', 'right');

set(htxt(1:3:end), 'color', 'w'); % for legibility

aliyar attaran
on 4 Nov 2016

for 2 bars one one y value:

for i2=1:numel(y)

tx(i2,1)=text(x(i2),y(i2,1),num2str(y(i2,1),'%0.2f'),...

'HorizontalAlignment','right',...

'VerticalAlignment','bottom');

tx(i2,2)=text(x(i2),y(i2,2),num2str(y(i2,2),'%0.2f'),...

'HorizontalAlignment','left',...

'VerticalAlignment','bottom');

end

##### 0 Comments

Elimelech Schreiber
on 5 Nov 2017

simply:

bar(x);

barvalues;

##### 3 Comments

Giuseppe Naselli
on 15 Jul 2022

Thanks for the barvalues function Elimelech, very easy to use and solid

Very appreciated

G

Halina H
on 7 Dec 2017

##### 1 Comment

Image Analyst
on 15 Jul 2022

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!