text label above the bars
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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'};
채택된 답변
Star Strider
2016년 7월 2일
0 개 추천
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
Rene Sebena
2016년 7월 2일
편집: Rene Sebena
2016년 7월 3일
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.
Star Strider
2016년 7월 2일
My pleasure!
Image Analyst
2016년 7월 3일
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;

Rene Sebena
2016년 7월 3일
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
2016년 7월 3일
편집: Image Analyst
2016년 7월 3일
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.
Rene Sebena
2016년 7월 3일
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".
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
