legend with multiple axes using yyaxis (2024)

169 views (last 30 days)

Show older comments

nathan blanc on 2 Jan 2021

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis

Commented: Star Strider on 3 Feb 2023

Accepted Answer: Star Strider

I am creating a plot with two different y axes. the curves are added alternately. for example:

yyaxis left

plot(A)

yyaxis right

plot(B)

yyaxis left

plot(C)

yyaxis right

plot(D)

My problem is that when I add a legend to the plot, the order of the curves is not the order in which they were created. instead, they are ordered first by the left axis

and then by the right axis. meaning if i write down "legend(A,B,C,D)" the legend entry B will correspond with the curve C and vice-versa. is there a way to fix this? The actual case there are more than 4 curves, and the number changes every time, so manual changing is not possible. many thanks in advance

Nathan

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Star Strider on 2 Jan 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#answer_589388

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#answer_589388

Edited: Star Strider on 2 Jan 2021

Open in MATLAB Online

Try this:

A = rand(1,10);

B = rand(1,15);

C = rand(1,20);

D = rand(1,30);

figure

hold on

yyaxis left

ha = plot(A);

yyaxis right

hb = plot(B);

yyaxis left

hc = plot(C);

yyaxis right

hd = plot(D);

hold off

legend([ha,hb,hc,hd], 'A','B','C','D')

Alternatively:

figure

yyaxis left

ha = plot(A);

hold on

hc = plot(C);

hold off

yyaxis right

hb = plot(B);

hold on

hd = plot(D);

hold off

legend([ha,hb,hc,hd], 'A','B','C','D')

Using hold is the only way to do what you want. This cannot be used with your posted code, because without the hold function, the second calls to yyaxis left and yyaxis right calls eliminate the handles to the previously plotted curves.

EDIT — (2 Jan 2021 at 17:41)

Added alternative code.

.

6 Comments

Show 4 older commentsHide 4 older comments

nathan blanc on 2 Jan 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_1240713

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_1240713

Thank you Star. I didn't quite understand why you kept holding on and off in your code, but the naming of the plots and addition to the legend command solved my problem

Nathan

Star Strider on 2 Jan 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_1240768

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_1240768

As always, my pleasure!

The hold function permits subsequent plots on the same axes, and that is necessary here. (I thought that I already posted a link to that function, however I now realise that did not, and have now corrected that. My apologies for the oversight.)

Giovanni Bambini on 2 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2595371

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2595371

Edited: Giovanni Bambini on 2 Feb 2023

Open in MATLAB Online

This does not seem to work when grouping bars:

A = [rand(10,1) zeros(10,1)];

B = [zeros(10,1) rand(10,1)];

figure;

yyaxis left

ha = bar(A, 'FaceColor', '#0000FF');

set(findobj(gcf,'type','axes'), 'ycolor', '#0000FF');

yyaxis right

hb = bar(B, 'FaceColor', '#00BCFF');

set(findobj(gcf,'type','axes'), 'ycolor', '#00BCFF');

legend([ha hb], 'h1', 'h2');

Warning: Ignoring extra legend entries.

legend with multiple axes using yyaxis (6)

Please help

E.

Voss on 2 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2595471

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2595471

Open in MATLAB Online

ha and hb are each 2 bar objects, as shown below. Therefore, [ha hb] is four bar objects. You can use just the first element of each ha and hb for the legend, [ha(1) hb(1)]

A = [rand(10,1) zeros(10,1)];

B = [zeros(10,1) rand(10,1)];

figure;

yyaxis left

ha = bar(A, 'FaceColor', '#0000FF')

ha =

1×2 Bar array: Bar Bar

set(findobj(gcf,'type','axes'), 'ycolor', '#0000FF');

yyaxis right

hb = bar(B, 'FaceColor', '#00BCFF')

hb =

1×2 Bar array: Bar Bar

set(findobj(gcf,'type','axes'), 'ycolor', '#00BCFF');

legend([ha(1) hb(1)], 'h1', 'h2');

legend with multiple axes using yyaxis (8)

Giovanni Bambini on 3 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2596721

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2596721

Thanks!!!!

Star Strider on 3 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2596936

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/707198-legend-with-multiple-axes-using-yyaxis#comment_2596936

My pleasure!

A Vote would be appreciated!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Find more on Legend in Help Center and File Exchange

Tags

  • yyaxis
  • legend
  • axes

Products

  • MATLAB

Release

R2020b

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.


legend with multiple axes using yyaxis (11)

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

legend with multiple axes using yyaxis (2024)

FAQs

How to add multiple y-axis in Matlab? ›

Create axes with a y-axis on both the left and right sides. Plot a set of data against the left y-axis. Then, use yyaxis right to activate the right side so that subsequent graphics functions target it. Plot a second set of data against the right y-axis and set the limits for the right y-axis.

How to plot 3 axis in Matlab? ›

plot3( X , Y , Z , LineSpec ) creates the plot using the specified line style, marker, and color. plot3( X 1, Y 1, Z 1,..., X n, Y n, Z n) plots multiple sets of coordinates on the same set of axes. Use this syntax as an alternative to specifying multiple sets as matrices.

How to add two legends in Matlab? ›

Direct link to this answer
  1. t = 0:0.1:5;
  2. y = sin(t);
  3. z = cos(t);
  4. figure; hold on;
  5. for i = 1:2.
  6. p1(i) = plot(t, y,'r');
  7. p2(i) = plot(t, z,'b');
  8. end.
Feb 22, 2023

How to have two y labels in Matlab? ›

Add a second y-axis to an existing chart using yyaxis . The existing plots and the left y-axis do not change colors. The right y-axis uses the next color in the axes color order. New plots added to the axes use the same color as the corresponding y-axis.

How to plot 3 variables in one plot? ›

A basic “color plot”" displays the values of three variables at a time using colored symbols, where the value of one variable determines the relative position of the symbol along the X-axis and the value of a second variable determines the relative position of the symbol along the Y-axis, and the value of the third ...

How do you add an XY axis in Matlab? ›

Direct link to this answer
  1. xL = xlim;
  2. yL = ylim;
  3. line([0 0], yL); %x-axis.
  4. line(xL, [0 0]); %y-axis.
Jun 12, 2012

How do you make a two column legend in MATLAB? ›

Legend with Multiple Columns

Add a legend with two columns by setting the NumColumns property to 2.

Can you have two legends on a map? ›

On the "map" visual you are only able to add one legend which is a color legend. Often times you need two legends. Several other BI tools like Tableau provide the option to have a legend based on "Shapes" and a legend based on "color".

How to manipulate legend in MATLAB? ›

To add a legend title, set the String property of the legend text object. To change the title appearance, such as the font style or color, set legend text properties. For a list, see Text Properties. plot(rand(3)); lgd = legend('line 1','line 2','line 3'); lgd.

What is the Yyaxis function in MATLAB? ›

The yyaxis function creates an Axes object with a y-axis on the left and right sides. Axes properties related to the y-axis have two values. However, MATLAB® gives access only to the value for the active side.

How do you add two Y axis in numbers? ›

If you want a line graph with 2 y-axes. You to insert->Chart-> 2-Axis. Put in your data. One of the sets will be a line the others a bar.

How do I add multiple Y axis in sheets? ›

Add a second Y-axis
  1. On your computer, open a spreadsheet in Google Sheets.
  2. Double-click the chart you want to change.
  3. At the right, click Customize.
  4. Click Series.
  5. Optional: Next to "Apply to," choose the data series you want to appear on the right axis.
  6. Under "Axis," choose Right axis.

How do you make multiple graphs appear in Matlab? ›

Accepted Answer

If you want to display all plots in the same figure, utilize 'hold on. ' For multiple sub-figures, consider using the 'subplot' function.

Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 6079

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.