how to retain the label of the tick and add a new label at any specified tick value

1 次查看(过去 30 天)
close all x = linspace(0,4*pi); y = sin(x); plot(x,y) axis([0 4*pi -1.2 1.2]) % Define y-ticks and their labels.. set(gca,’yTick’,-0.5) set(gca,’yTickLabel’,{’abc’}); %% this labels the y axis. %% now add another label set(gca,’yTick’,0.5) set(gca,’yTickLabel’,{’def’});
The problem is the command to set the new label deletes the previous labels and adds the new one. I want add a new label while retaining the previous labels. how to get that ???

回答(2 个)

Star Strider
Star Strider 2015-8-4
编辑:Star Strider 2015-8-4
There is no way to do what you want, even using the hold function. You have to combine the tick labels together in the same call:
x = linspace(0,4*pi);
y = sin(x);
plot(x,y)
axis([0 4*pi -1.2 1.2])
% Define y-ticks and their labels..
set(gca,'yTick',[-0.5 0.5], 'yTickLabel',{'abc', 'def'})

Kelly Kearney
Kelly Kearney 2015-8-4
If you want to keep the original numeric ticks as well, the following will do that:
x = linspace(0,4*pi);
y = sin(x);
plot(x,y);
axis([0 4*pi -1.2 1.2]);
ytick = get(gca, 'ytick');
yticklab = cellstr(num2str(ytick'));
ytick = [ytick -0.5 0.5];
yticklab = [yticklab' 'abc' 'def'];
[ytick,ia] = unique(ytick, 'last');
yticklab = yticklab(ia);
set(gca, 'ytick', ytick, ...
'yticklabel', yticklab);
  2 个评论
Pankaj Jha
Pankaj Jha 2015-8-4
Thanks for the reply. I want my
yTick=[0.1 0.2 -0.3 -0.5 0.4];yTickLabel=[ab cd ef gh ij];
and I want to maintain the order in which they are labeled. plz help.
Kelly Kearney
Kelly Kearney 2015-8-5
Not quite sure what you mean by maintain the order... something like this?
x = linspace(0,4*pi);
y = sin(x);
plot(x,y);
axis([0 4*pi -1.2 1.2]);
yTick = [0.1 0.2 -0.3 -0.5 0.4];
yTickLabel = {'ab' 'cd' 'ef' 'gh' 'ij'};
[yTick, isrt] = sort(yTick);
set(gca, 'ytick', yTick, 'yticklabel', yTickLabel(isrt));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by