How to show two axis on a plot only where i want them

Hi Matlab Gurus,
I have the plot below, and i have two y axis, one on the left and one on the right, which are different to each other.
What i would like to do is only show the numbers on the y-axis (left and right), where the horizontal lines are
So i can draw a horizontal line using the left axix, and i can draw another horizonal line using the right axis.
then i want the axis to only show exactly where those lines are, not evenly spread based on itself, but only where the lines are, so that i know the exact value of the lines at both the left and right axis.
Any help would be highly appreciated.

 Accepted Answer

You can get the handles for the left and right axes with yyaxis.
yyaxis left; % Sets the current y-axis left
left_ax = gca; % Creates "left_ax" variable which stores the current (left) y-axis
yyaxis right; % Sets the current y-axis right
right_ax = gca; % Creates "right_ax" variable which stores the current (right) y-axis
With these handles, you can find and edit pretty much any feature of the axis. For Tick Labels
left_ax.YTick = linspace(0,1,5); % Create 5 equally spaced ticks, for example
If your horizontal lines are meant to be grid lines, you can simple add them with
grid on
and they should conform to the tick labels on the active axis.

5 Comments

Dear Sir,
I'm not trying to use grid lines, i have my own horizontal lines at specific levels.
The approach above worked for my left axis, but it didn't do it for the right axis
See my code below
yyaxis left; % Sets the current y-axis left
left_ax = gca; % Creates "left_ax" variable which stores the current (left) y-axis
left_ax.YTick = linspace(0,1,9);
yyaxis right;
right_ax = gca;
right_ax.YTick = linspace(1,1000,9);
Now see the result below.
It does exactly what i wanted for the left axis, it puts the axis along 9 discrete points and labels them how i wanted
One the right i would have expected the same but different numbers as per (linespace(1,1000,9), but i got nothing on the right? Maybe it needs to have its scale updated or something?
Try disp(right_ax.YTick) and disp(right_ax.ylim).
It’s possible the YTicks are correct, but out of the range of the y limits. If so, you’d just have to reset the ylim field.
Dear Sir,
I've added the lines of code you noted, it displays those values, but not at the right axis of the plot.
Plesae see below
yyaxis left; % Sets the current y-axis left
left_ax = gca; % Creates "left_ax" variable which stores the current (left) y-axis
left_ax.YTick = linspace(0,1,9);
yyaxis right;
right_ax = gca;
right_ax.YTick = linspace(1,1000,9);
disp(right_ax.YTick);
disp(right_ax.YLim);
Dear Sir,
With your help i have worked it out, you were correct, i needed to set the YLim property.
Can i ask, i want to set this propoerty to be auto, so it can automaticaly work it out based on the data, how can i do that?
I tried right_ax.YLim = 'auto', but that seems to just go back to the initial problem and set the axis similar to left hand Y axis.
yyaxis right;
right_ax = gca;
right_ax.YTick = linspace(min(PriceAtLevel),max(PriceAtLevel),9);
right_ax.YLim = [min(PriceAtLevel) max(PriceAtLevel)];
right_ax.ylim = [min(right_ax.YTick), max(right_ax.YTick)];
should work, after YTick has been set.

Sign in to comment.

More Answers (1)

try this yticks
h = plot(0:10,sin(0:10));
yy = 0:0.2:1;
[x,y] = ndgrid([0 10],yy);
line(x,y)
yticks(yy)

1 Comment

Dear Sir,
How will you now put a different scale on the right with different horizontal lines?
Or even better, how can i use the same horizonal lines above, but with a different scale on the right?
so the same pic above, but at the right the values of the lines are different, for eg (0, 2, 4, 6, 8, 10) .

Sign in to comment.

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!