如何标记y轴上的位置?

时间:2017-04-23 03:46:20

标签: matlab plot matlab-figure axis-labels

enter image description here

如上图所示,我想在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 个答案:

答案 0 :(得分:2)

在MATLAB 2016b中引入了

yticksyticklabels

对于早期版本,要添加额外的y-ticks以及当前的y-ticks,并更改问题中的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

答案 1 :(得分: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