2017-04-23 109 views
1

enter image description here如何標記y軸上的位置?

如上圖所示,我想標記y軸上的兩個位置爲「y = 60」和「y = -60」。

我嘗試執行命令

yticks([-60 0 60]); 
yticklabels({'y = -60','y = 0','y = 60'}) 

但是,它揭示了「沒有變yticks」。另外,我還想在y軸上添加滴答作爲[-60 -40 -20 0 20 40 60]。

回答

2

yticksyticklabels在MATLAB 2016B進行了介紹。

對於早期版本,添加額外的y蜱非常久遠當前y蜱,並修改y-TICK標籤就像那些有問題,你可以使用這個:

set(gca, 'YTick', unique([-60, 60, get(gca, 'YTick')])); 
%-60 and 60 are the additional ticks that'll be added to y axis. 
%unique is applied just in case if the tick/s, that we want to add, already exist/s 
%and to sort the array in ascending order. unique also does the sorting in ascending order 
%if you want to show only specific yticks, use the following instead: 
%set(gca,'YTick',[-60 -40 -20 0 20 40 60]); %to show only [-60 -40 -20 0 20 40 60] yticks 
temp1=get(gca,'Yticklabels'); %Getting Yticklabels 
temp2=str2double(temp1);  %Converting them to double 
%Searching for desired labels and concatenating them with 'y = ' 
temp1(temp2==-60|temp2==60)= strcat({'y = '},{'-60','60'}); 
set(gca,'YTickLabel',temp1); %Setting the Yticklabels 
+0

非常感謝。對我很有幫助。 – FortranFun

0

你可以像下面這樣做

scatter(0:5,0:5+rand(5,1)) 

yticks([0 2.5 5]) 
set(gca,'YTickLabel',{'y=0', 'y=2.5', 'y=5'}) 

enter image description here

+0

它不工作。它揭示了一個錯誤,即「未定義的函數或變量'yticks'」。 – FortranFun

+0

什麼是你的matlab版本? –

+0

這是Matlab R2016 a – FortranFun