How to set front and back lines on plot with yyaxis left and right

73 views (last 30 days)
Hi,
I have a situation where I would like the plot of the right axis (yyaxis right) to be "under" the plot from the left axis (yyaxis left). Here is a simplified code sample:
x = 1:0.001:100;
y = sin(x);
y2 = awgn(y,2); % Add noise
figure(1)
yyaxis right
p2 = plot(x,y2);
yyaxis left
p1 = plot(x,y);
legend([p2,p1],'Curve that should be behind','curve that should be in front')
The problem (I think) is that the left axis is called first no matter what. So even if I specify the right axis first, the left curve will still be under the one on the right. I am not able to change this using children of the figure (specify the right axis to be plotted first instead of left).
In my application, I would really like to keep the curve on their side, because it would be uniform with other figure in my article. What happens is that the curve on the right is noisier than the one on the left, which is why I would like to have it under in this format (using yyaxis).
Thanks in advance for any ideas you could have!

Accepted Answer

Kevin Holly
Kevin Holly on 1 Mar 2022
Edited: Kevin Holly on 2 Mar 2022
That is interesting. While I try and figure out if there is a solution to this, you can fake it til you make it with this:
x = 1:0.001:100;
y = sin(x);
y2 = awgn(y,2); % Add noise
% yyaxis right
p2 = plot(x,y2/5,'Color',[0.8500 0.3250 0.0980]);
h=gca;
h.YTickLabel = {-1:0.2:1};
ylim([-1 1])
hold on
yyaxis right
p1 = plot(x,y*5,'Color',[0 0.4470 0.7410]);
h=gca;
h.YTickLabel = {-4:1:5};
ylim([-4 5])
legend([p2,p1],'Curve that should be behind','curve that should be in front')
yyaxis left
h.YColor = [0 0.4470 0.7410];
Edit: Here is a method to switch the axis labels, but then the ticks are off.
figure(2)
x = 1:0.001:100;
y = sin(x);
y2 = awgn(y,2); % Add noise
yyaxis right
p2 = plot(x,y,'Color',[0 0.4470 0.7410]);
yyaxis left
p1 = plot(x,y2,'Color',[0.8500 0.3250 0.0980]);
hl=gca;
l_yaxis = hl.YTickLabel;
yyaxis right
hr=gca;
r_yaxis = hr.YTickLabel;
hr.YTickLabel = l_yaxis;
yyaxis left
hl.YTickLabel = r_yaxis;
legend([p2,p1],'Curve that should be behind','curve that should be in front')
This has been a known isssue (see link below). I'll add you to the list to recommend this feature.
  2 Comments
Kevin Holly
Kevin Holly on 2 Mar 2022
Here is a workaround:
figure(1)
x = 1:0.001:100;
y = sin(x);
y2 = awgn(y,2); % Add noise
right_axes = axes;
p1 = plot(right_axes,x,y2,'Color',[0.8500 0.3250 0.0980]);
right_axes.YAxisLocation = 'right';
right_axes.YAxis.Color = [0.8500 0.3250 0.0980];
left_axes = axes;
p2 = plot(left_axes,x,y,'Color',[0 0.4470 0.7410]);
left_axes.Color = 'none';
left_axes.YAxis.Color = [0 0.4470 0.7410];
legend([p1,p2],'Curve that should be behind','curve that should be in front')
Gab D
Gab D on 2 Mar 2022
Hello Kevin, thank you for your prompt answers!
I did tried creating new axes and using them in plot, but I could not make it work. Your method is quite direct and simple and direct and when I tried it in my example, it works fine.
However, since we define new axes, I had some difficulty with overlaps of the original and newly created axes. In the end, I used your second solution, still using yyaxis and it works fine. I just had to redefine the Tick and TickLabel for each side of the graph.
Forr anyone interrested, the final view of my graph is inn attachment.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!