WJA
WJA

Reputation: 7004

Replace yaxis values with text

I have a list of labels:

lnames = {'nameA','nameB','nameC'}

and a vector with indices:

Y = [1; 1; 2; 3; 2];

index(1) refers to nameA, index(2) to nameB etc.

I want to show when a certain name is selected:

plot(Y, 'd')

This gives me a plot (on bigger scale as follows):

enter image description here

However, I want to replace the numerical values on the yaxis with the labels. Thus 1 becomes nameA, 2 becomes nameB, etc. It would look as follows:

enter image description here

How can this be achieved?

Upvotes: 1

Views: 82

Answers (2)

Md Johirul Islam
Md Johirul Islam

Reputation: 5162

Y = [1; 1; 2; 3; 2];
yticks([1 2 3])
yticklabels({'nameA', 'nameB', 'nameC'})
plot(Y, 'd');

Upvotes: 0

jodag
jodag

Reputation: 22314

You can specify the YTick and YTickLabel for the axis of your plot as follows.

h = plot(Y, 'd');
h.Parent.YTick = 1:numel(lnames);
h.Parent.YTickLabel = lnames;

Upvotes: 2

Related Questions