legend with multiple axes using yyaxis (2024)

187 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)
Top Articles
Game Akin To Bingo Nyt
Top 15 Luxury Kitchen Appliance Brands Reviewed - Appliances Labs
Ups Notary Store Near Me
Ink Free News Kosciusko County
Gasbuddy Costco Hawthorne
ACTS Occupational and Physical Therapy
Shadle Park big-play combo of Hooper-to-Boston too much for Mt. Spokane in 20-16 win
Climate change, eroding shorelines and the race to save Indigenous history - The Weather Network
Start EN - Casimir Pulaski Foundation
Seattle Clipper Vacations Ferry Terminal Amtrak
Munsif Epaper Urdu Daily Online Today
SSD an SATA Anschluss bei Futro S920
Promiseb Discontinued
Local Dog Boarding Kennels Near Me
Uscis Fort Myers 3850 Colonial Blvd
Caldwell Idaho Craigslist
Uw Oshkosh Wrestling
Bbaexclusive
Car Complaints Toyota
Ice Crates Terraria
JPMorgan and 6 More Companies That Are Hiring in 2024, Defying the Layoffs Trend
Brise Stocktwits
Best Chinese Rome Ny
Sugar And Spice Playboy Magazine
Watch The Lovely Bones Online Free 123Movies
[마감]봄나들이 갈때 나만의 스타일을 골라보아요~!마감된이벤트 - dodry
Joanna Gaines Reveals Who Bought the 'Fixer Upper' Lake House and Her Favorite Features of the Milestone Project
افضل موقع سكسي عربي
Lvc Final Exam Schedule
Roomba I3 Sealing Problem With Clean Base
Umbc Registrar
Jeep Graphics Ideas
Hawkview Retreat Pa Cost
Operation Carpe Noctem
Https Eresponse Tarrantcounty Com
Locals Canna House Deals
Indian Restaurants In Cape Cod
Shs Games 1V1 Lol
Phase 3 Cataclysm Classic New Changes, Preparation and Investments Guide
Juicy Deal D-Art
Enlightenment Egg Calculator
Morning Call Obits Today Legacy
Nz Herald Obituary Notices
Margie's Money Saver Hey Dudes
Nashville Predators Wiki
Naviance Hpisd
Lucky Money Strain
Smartmove Internet Provider
Breakroom Bw
Yoshidakins
Skagit.craigslist
Tattoo Shops Buckhannon Wv
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 5648

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.