필터 지우기
필터 지우기

how to assign names to each bar of a bar chart

조회 수: 367 (최근 30일)
Kirk
Kirk 2012년 3월 28일
댓글: ZenithWoman 2021년 6월 27일
Hello, Suppose i have some data that i want to plot in a bar chart. How do i assign labels to each of the bars. e.g.
somedata=randn(1,3)
somenames={'mike'; 'steve'; 'alex' }
barh(somedata)
My question is, how do i label each of the bars to the extreme left with the names.
Thank you for your help.

채택된 답변

C.J. Harris
C.J. Harris 2012년 3월 28일
You can simply add the names as the y-tick labels.
somedata=randn(1,3);
somenames={'mike'; 'steve'; 'alex' };
barh(somedata)
set(gca,'yticklabel',somenames)
  댓글 수: 1
emjey
emjey 2019년 5월 17일
편집: emjey 2019년 5월 17일
But that doens't work if you have more than 20-30 data records :(
In such case Matlab plots the first few labels only...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

ORUGANTI SIVAMURALIKRISHNA
ORUGANTI SIVAMURALIKRISHNA 2020년 7월 5일
Use like this
X = categorical({'Small','Medium','Large','Extra Large'});
Y = [0 15 20 25];
bar(X,Y)
  댓글 수: 2
Bill Tubbs
Bill Tubbs 2021년 6월 15일
Is there a reason why it sorts the data along the x-axis in alphabetical order? Is there any way to preserve the order?
ZenithWoman
ZenithWoman 2021년 6월 27일
To preserve the order write
Write like this
X = categorical({'Small','Medium','Large','Extra Large'});
X = reordercats(X,{'Small','Medium','Large','Extra Large'});
Y = [0 15 20 25];
bar(X,Y)
I hope this helps

댓글을 달려면 로그인하십시오.


Sarul Gupta
Sarul Gupta 2017년 5월 4일
Thanks :)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by