Is it possible to change label names in Matlab

조회 수: 10 (최근 30일)
Moe
Moe 2015년 6월 24일
댓글: Moe 2015년 6월 24일
I want to have different names in my Y axis rather than numbering like the following photo:

채택된 답변

Chad Greene
Chad Greene 2015년 6월 24일
Here I'll do a barh(A) where A is not sorted, and I'll label the different groups based on the longest bar. Then sort according to the size of the first column in A while preserving the group label association. I adjusted the values in your A a little bit to make it more clear:
A = [2,6,50;3,4,90;1,6,103];
longbar = {'shortest','middle','longest'};
subplot(211)
barh(A)
set(gca,'ytick',1:3,'yticklabel',longbar)
title 'unsorted data:'
box off
% Resize based on first column:
[values, order] = sort(A(:,1));
sortedmatrix = A(order,:)
subplot(212)
barh(sortedmatrix)
set(gca,'ytick',1:3,'yticklabel',longbar(order))
title 'sorted data:'
box off

추가 답변 (3개)

Sean de Wolski
Sean de Wolski 2015년 6월 24일
Adjust the axes' 'YTickLabel' property:
barh(rand(3,1));
ax = gca;
ax.YTickLabel = {'Hello','World','Wednesday'}
  댓글 수: 1
Moe
Moe 2015년 6월 24일
Thanks Sean. Actually my matrix is a little bit tricky. For example matrix A is as follows:
A = [2,6,100;3,4,101;1,6,103];
I will sort matrix A based on the column 1:
[values, order] = sort(matrix(:,1));
sortedmatrix = matrix(order,:)
So, I have now:
A = [1,6,103;2,6,100;3,4,101];
Now, I need a barh graph that the Y axis label is named base on the third column in sorted matrix A. And also is it possible to replace this new label with other names. For example, if I know 100 = Hello, 101 = World and 103 = Wednesday, then I need these new labels to be appear in my graph.

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


Chad Greene
Chad Greene 2015년 6월 24일
plot(1:9,1:9)
set(gca,'ytick',1:9,'yticklabel',{'book','pen','vb','top','red','green','black','green','verb'})

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 24일
t=0:10;
y=sin(t)
plot(y)
s={'a' 'b' 'c' 'd' 'e' 'f'}
yt=get(gca,'ytick')
n=numel(s)
set(gca,'xtick',linspace(min(yt),max(xt),n),'yticklabel',s')

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by