text label above the bars

Hi there,
I am new in matlab and I am trying to put text labels above the bars, so that A is above first one, B above second one etc...Can you help me with this
thank you
R
y = [10,20,30,15];
a = bar(y);
labels = {'A', 'B', 'C', 'D'};

 Accepted Answer

This works:
y = [10,20,30,15];
a = bar(y);
labels = {'A', 'B', 'C', 'D'};
xt = get(gca, 'XTick');
text(xt, y, labels, 'HorizontalAlignment','center', 'VerticalAlignment','bottom')

8 Comments

Rene Sebena
Rene Sebena on 2 Jul 2016
Edited: Rene Sebena on 3 Jul 2016
nice :) thank you very much, this help me a lot!
but I just find out that my task is a little bit more complicated.. I have data from several subjects and 4 exp. conditions eg: y = [10 15, 20 15,30 15,15 12];
a = bar(y);
and I have to put string labels above each subjects bar (for the same subject bar always the same label, so above 10, 20, 30 and 15 I have to put for example A and above 15,15,15 and 12 label B). Have can I solve this issue?? Thank you.
My pleasure!
Rene:
You can use sprintf() to create any string you want. Then just use text() to place that string in the desired location.
@Rene Sebena —
It’s one step more complicated than that. You have to split the string created by sprintf into a cell array of strings.
I’m lost as to what you’re asking. This will do what I believe you asked for, but you will have to experiment with it, since I’m not clear on what you want:
y = [10,20,30,15];
a = bar(y);
labels = {'A', 'B', 'C', 'D'};
nrs = [10, 20, 30, 15; 15, 15, 15, 12]; % Y-Coordinates For Each Set Of Bars
for k1 = 1:2
lblstr{k1} = sprintf([labels{k1} ' %.0f\n'], nrs(k1,:)); % Labels For Bars
end
xt = get(gca, 'XTick');
for k1 = 1:1 % Increase This To ‘1:2’ For The Second Set Of Bars
labels = regexp(lblstr{k1}, '\n', 'split');
text(xt, y, labels(1:end-1), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
end
Rene, to do what you asked last, you don't even need to mess with sprintf() or cell arrays. Just do this:
y = [10 15, 20 15,30 15,15 12];
a = bar(y);
xt = get(gca, 'XTick');
for k1 = 1:length(y)
if rem(k1,2) == 1
thisLabel = 'A'
else
thisLabel = 'B'
end
text(xt(k1), y(k1), thisLabel, ...
'FontSize', 20,...
'HorizontalAlignment','center', ...
'VerticalAlignment','bottom')
end
grid on;
Thank you for this, this is almost what I need, is there such an elegant solution for this kind of graph?
y = [10 15; 20 15;30 15;15 12];
I need to put A above each first bar(blue) of the group and B on the second ones(red).
My fault I did not explain it correctly at the beginning. Sorry for that.
Image Analyst
Image Analyst on 3 Jul 2016
Edited: Image Analyst on 3 Jul 2016
You've never mentioned anything about red or blue before. What is read and blue - the text or the bars or both? And what is the "second one of the group"? I see just one group of 8 bars and they're all blue.
When you said "so above 10, 20, 30 and 15 I have to put for example A and above 15,15,15 and 12 label B" it seemed to us that you wanted bar #'s 1,3,5, and 7 to have an A above them, and bars 2,4,6, and 8 to have a B above them (at least that was a guess on my part that matched what you said). Now you've made it confusing again.
Yes, my fault, sorry for that, I have groups of bars for several subjects and 4 experimental conditions,.. Subject1 results are [10,20,30,15] and subject2 results are [15,15,15,12] so the matrix is like this:
y = [10 15; 20 15;30 15; 15 12];
and I just need to label subject 1 results with eg. "A" and subject 2 results with label "B".

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!