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

41 views (last 30 days)

Show older comments

Gab D on 1 Mar 2022

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right

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')

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

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

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

  • Link

    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

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right#answer_907425

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right#answer_907425

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];

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

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')

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

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

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

  • Link

    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')

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

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

  • Link

    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

Tags

  • yyaxis
  • front and back items
  • noise
  • figure
  • axes

Products

  • MATLAB

Release

R2021b

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.


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

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

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

FAQs

How to plot two graphs with different y-axis in MATLAB? ›

Plot Multiple Sets of Data on Each Side

Plot two lines against the left y-axis by using the hold on command. Plot two lines against the right y-axis. The hold command affects both the left and right y-axes, so you do not need to reissue it. After plotting, turn hold back off.

How to plot two lines in MATLAB? ›

Plot Multiple Lines

Use the figure command to open a new figure window. You can plot multiple lines using the hold on command. Until you use hold off or close the window, all plots appear in the current figure window.

How to rescale the y-axis in MATLAB? ›

yscale( scale ) sets the scale of the y-axis to be linear or logarithmic in the current axes. Specify scale as "linear" or "log" . You can also omit the parentheses and quotation marks when using this syntax. For example, yscale log is equivalent to yscale("log") .

How to change axis values in MATLAB plot? ›

You can change the x- or y-axis units by right-clicking the mouse on the axis label or by right-clicking on the plot and selecting Analysis Parameters.

Can you have 2 Y axis on a graph? ›

When the data values in a chart vary widely from data series to data series, or when you have mixed types of data (for example, currency and percentages), you can plot one or more data series on a secondary vertical (Y) axis. The scale of the secondary Y-axis reflects the values for the associated data series.

How to plot a graph with 2 variables in MATLAB? ›

Here is a sample code to plot your functions:
  1. % Define the functions.
  2. f1 = @(x, y) x.^3 - y.^2 - 1;
  3. f2 = @(x, y) 0.5 + cos(x).*tanh(y);
  4. % Create a new figure.
  5. figure;
  6. % Plot the first function.
  7. fimplicit(f1, [-2, 2, -2, 2], 'r');
  8. hold on;
Mar 5, 2024

How to plot two bars in MATLAB? ›

To plot a single series of bars, specify y as a vector of length m. The bars are positioned from 1 to m along the x-axis. To plot multiple series of bars, specify y as a matrix with one column for each series.

How to put a line on a graph in MATLAB? ›

yline( y ) creates a horizontal line at one or more y-coordinates in the current axes. For example, yline(2) creates a line at y=2 . yline( y , LineSpec ) specifies the line style, the line color, or both. For example, yline([12 20 33],'--b') creates three dashed blue lines.

How do you set the Y-axis scale? ›

Changing the scale of the axis: You can customize and modify the overall scale, interval and range of each axis in Excel using the "Format Axis" menu. Open the menu by right-clicking on an axis, then input the minimum and maximum values for the axis in the dialog box.

How do you set limits on the Y-axis in MATLAB? ›

ylim( limits ) sets the y-axis limits for the current axes or chart. Specify limits as a two-element vector of the form [ymin ymax] , where ymax is greater than ymin . ylim( limitmethod ) specifies the limit method MATLAB® uses for automatic limit selection.

How do you rescale the Y-axis in origin? ›

To change the axis scale range manually: Double-click on the X, Y, or Z axis to open the respective Axis dialog box. Select the Scale tab. Select the axis icon from the left panel.

How do you change axis bounds? ›

Right-click the axis then select Format Axis in the menu. The Format Axis panel appears on the right side of the window. Under Axis Options in the Format Axis panel, set the minimum and maximum axis values by entering a value in the Minimum and Maximum boxes under Bounds.

How do you set the axis on a plot? ›

Change Axis Limits

Create a line plot. Specify the axis limits using the xlim and ylim functions. For 3-D plots, use the zlim function. Pass the functions a two-element vector of the form [min max] .

How to change line width in Matlab? ›

Specify the line width by setting the “LineWidth” property a name-value pair.
  1. plot(x1,y1,'LineWidth',5)
  2. hold on.
  3. plot(x2,y2,'LineWidth',10)
  4. hold off.
Sep 22, 2011

Can I plot 2 graphs in Matlab? ›

Combine Plots in Same Axes

However, you can use the hold on command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off. When the hold state is on, new plots do not clear existing plots or reset axes properties, such as the title or axis labels.

How to plot a graph with 2 variables in matlab? ›

Here is a sample code to plot your functions:
  1. % Define the functions.
  2. f1 = @(x, y) x.^3 - y.^2 - 1;
  3. f2 = @(x, y) 0.5 + cos(x).*tanh(y);
  4. % Create a new figure.
  5. figure;
  6. % Plot the first function.
  7. fimplicit(f1, [-2, 2, -2, 2], 'r');
  8. hold on;
Mar 5, 2024

How do you plot two bar graphs together in Matlab? ›

Set the bar width to 0.5 so that the bars use 50% of the available space. Specify the bar color by setting the FaceColor property to an RGB color value. Plot a second bar graph over the first bar graph. Use the hold function to retain the first graph.

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5792

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.