Labels above bar-plot
조회 수: 44 (최근 30일)
이전 댓글 표시
Hello,
I have a matrix A (1X40). For this matrix I make its bar-plot. Aslo, I have the matrix B (1X40) which contains values, which I want to put as an label above each bar. I wrote a small code, but the values in matrix B do not appear (such label) above each bar. Below is the code.
bar(A) % 1X40
labels = arrayfun(@(value) num2str(values,'0.0f'),B,'UniformOutput',false);
text(A,B,labels,HorizontalAlignment','center',VerticalAlignment','bottom')
Your help is important !!!
댓글 수: 0
채택된 답변
Riccardo Scorretti
2022년 3월 25일
Hi. Up to my understanding, the problem seems to be that when you use bar(A) the coordinates along x are 1, 2, ... 40. Assuming that variables A, B, and labels are correctly defined you could try this:
text(1:40, A, labels, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
For instance:
A = rand(1,40); B = rand(1,40);
labels = arrayfun(@(value) num2str(value,'%0.0f'),B,'UniformOutput',false);
bar(A);
text(1:40, A, labels, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
추가 답변 (2개)
Chunru
2022년 3월 25일
A = 1:10;
B = randn(1, 10)*10;
bar(A, B, 0.5);
labels = compose('%.0f', B);
text(A, B, labels, 'HorizontalAlignment','center','VerticalAlignment','bottom')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!