Two Y axis plot priority

5 views (last 30 days)
B R
B R on 2 Mar 2023
Edited: Voss on 2 Mar 2023
I am trying to create a plot with two vertical axes, in which the line associated with the left vertical axis is plotted OVER the line associated with the right vertical axis. How do I bring the line associated with the left vertical axis to the front?
Sample Code:
x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = cos(x);
figure, plot(x,y1,'linewidth',3) % Large linewidth so you can see which line is in front
yyaxis right, plot(x,y2,'linewidth',3)
Thanks.

Accepted Answer

Cameron
Cameron on 2 Mar 2023
There's no great way to do it. You have to trick the plot into thinking there is Z data.
x = linspace(0,2*pi,100);
y1 = sin(x);
y2 = cos(x);
fig = figure;
ax = axes(fig);
yyaxis left
p1 = plot(x,y1,'linewidth',3); % Large linewidth so you can see which line is in front
yyaxis right
p2 = plot(x,y2,'linewidth',3);
p1.ZData = ones(length(p1.YData),1);
p2.ZData = zeros(length(p1.YData),1);
ax.SortMethod = 'depth';

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!