How to add yTickLabels to multi plot graph?

Hello, please have a look at the attached pic. I have 25 plots, drawn on 1 figure (you can 25 raws...)
In the bottom-left corner, I've added with Paint, numbers (1,2,3,4,...).
Question is: how can I add this in Matlab without destroying my y Axis values? (what you can there as 50,100,150...)
Thanks

 Accepted Answer

See the documentation on Axes Properties (link), particularly XTick (link) and XTickLabel (link). (I know you’re doing y-ticks, but it works the same for both.)

6 Comments

thank you. But my problem is, how do I create a double y-axis? I know there is an option for yyaxis. Issue is I don't want one y-axis on the left and one on the right. I want 2 yTickLabels on the left side.
Is there a way to do it?
My pleasure.
You would have to create a vertical line and horizontal tick marks with plot, and then use the text function for the tick labels. None of these are necessarily difficult, but they do require specific programming for each step (vertical axis line, horizontal ticks, and text labels).
I don't sure I follow you. Can you please post some lines of code.
Here you go:
xloc = repmat([0.48 0.5], 5, 1)'; % X-Location Of Vertical Bar In Axes
ytix = repmat(linspace(0.25,0.75,5)', 1, 2)'; % Y-Location Of Tick Bars In Axes
figure(1)
plot([1 1]*0.5, [0.25 0.75], 'k') % Draw Vertical Axis Line
hold on
plot(xloc, ytix, '-r') % Draw Tick Lines
hold off
lbls = regexp(sprintf('%.1f \n',1:5), '\n', 'split'); % Tick Label String
text(xloc(1,:), ytix(1,:), lbls(1:end-1), 'VerticalAlignment','middle', 'HorizontalAlignment','right')
axis([0 1 0 1])
Thank you Star. Question is, how do I make this (1,2,3,...) new bar, to be spaced according to the ySpacing of my 25 plots. You can see in the image I've attached, some plots are close to each other, while between others there is a much larger gap (in y-axis).
Also, I assume, it is possible to create this new labels bar outside of my figure, to the left of the yTicks, right?
My pleasure.
You just have to experiment with the spacing of your plots in the figure window. You can probably create a function file from my code, and then just call it with the appropriate arguments for each plot.
‘I assume, it is possible to create this new labels bar outside of my figure, to the left of the yTicks, right?’
You assume incorrectly. To the best of my knowledge, you cannot plot outside the plot box. You can see this by changing the axis call in my code to:
axis([0.6 1 0 1])
The line disappears.
I would create two separate y-axes with my code, then turn the default y-axis off. See Axes Properties for details on how to do that. It’s not technically difficult, but does involve programming and experimentation, so make time for that.

Sign in to comment.

More Answers (1)

Why not just add ticks at 1, 2, 3, 4, etc. in addition to those that already exist? Change the YTick property of the axes to do that.

3 Comments

it won't be in the same scale... The plots values goes up to 300, while I want to number them from 1 to 25.
Nothing says that ticks must be equally spaced.
x = 1:10;
y = x.^2;
h = plot(x, y);
ax = ancestor(h, 'axes');
% Look at the ticks on the axes before running the next line
ax.YTick = y;
Nothing says that the tick labels must be the same as the tick values, either.
r = randperm(10) % Arbitrary values
labels = arrayfun(@num2str, r, 'UniformOutput', false);
ax.YTickLabel = labels;
Thank you Steven. Is there a way to somehow differentiate between the YTickLabels and the YTicks? Maybe different color somehow? Or perhaps it's possible to distant a little bit the YTickLabels, to be slightly on the left of the YTicks?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!