Labels above bar-plot

50 ビュー (過去 30 日間)
stelios loizidis
stelios loizidis 2022 年 3 月 25 日
コメント済み: stelios loizidis 2022 年 3 月 25 日
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 !!!

採用された回答

Riccardo Scorretti
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');
  1 件のコメント
stelios loizidis
stelios loizidis 2022 年 3 月 25 日
It works!!!! Thanks for the valuable help !!!!

サインインしてコメントする。

その他の回答 (2 件)

Arif Hoq
Arif Hoq 2022 年 3 月 25 日
編集済み: Arif Hoq 2022 年 3 月 25 日
please expand the plot for a clear view.
A=1:5:200;
B=1:40;
bar(B)
text(1:length(A),B,num2str(B'),'VerticalAlignment','bottom','HorizontalAlignment','center');
box off

Chunru
Chunru 2022 年 3 月 25 日
A = 1:10;
B = randn(1, 10)*10;
bar(A, B, 0.5);
labels = compose('%.0f', B);
labels = 1×10 cell array
{'9'} {'8'} {'-3'} {'-7'} {'12'} {'-31'} {'-2'} {'9'} {'-8'} {'-11'}
text(A, B, labels, 'HorizontalAlignment','center','VerticalAlignment','bottom')

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by