41 views (last 30 days)
Show older comments
Gab D on 1 Mar 2022
Commented: Gab D on 2 Mar 2022
Accepted Answer: Kevin Holly
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Adam Danz on 2 Mar 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right#comment_2015955
I'll add your thread to the list....
https://www.mathworks.com/matlabcentral/answers/855500-uistack-giving-error-with-yyaxis#comment_1582335
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Kevin Holly on 1 Mar 2022
Edited: Kevin Holly on 2 Mar 2022
Open in MATLAB Online
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.
https://www.mathworks.com/matlabcentral/answers/855500-uistack-giving-error-with-yyaxis#comment_1582335
2 Comments Show NoneHide None
Show NoneHide None
Kevin Holly on 2 Mar 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right#comment_2015950
Open in MATLAB Online
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 on 2 Mar 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right#comment_2016725
- UFOA_CFOA_T_Tdot_H.jpg
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)
Sign in to answer this question.
See Also
Categories
MATLABGraphics2-D and 3-D Plots
Find more on 2-D and 3-D Plots 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office