How to set front and back lines on plot with yyaxis left and right
45 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
1 commentaire
Réponse acceptée
Kevin Holly
le 1 Mar 2022
Modifié(e) : Kevin Holly
le 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 commentaires
Kevin Holly
le 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')
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!