How to change the order of the legend in the Matlab version 2019b? (2024)

127 views (last 30 days)

Show older comments

Gopinath Karuppannan on 29 Oct 2021

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b

Commented: Gopinath Karuppannan on 30 Oct 2021

Accepted Answer: Dave B

  • check.fig

Open in MATLAB Online

Hi

I am trying to change the order of legends based upon the user needs. Below is the reference figure and codes (for eg.)

plot([1 2],[15,30])

>> hold on;plot([1 2],[5,35]);

>> hold on;plot([1 2],[40 40],'--k');

How to change the order of legend in the attached figure?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Dave B on 29 Oct 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#answer_819418

Edited: Dave B on 29 Oct 2021

Open in MATLAB Online

  • check.fig

With your existing .fig file, grab the line objects and feed them into legend in whatever order you like:

open('check.fig')

kids=findobj(gca,'type','Line');

legend(kids)

How to change the order of the legend in the Matlab version 2019b? (3)

% or legend(kids([2 3 1])) if that's the order you like

% or legend(kids,'abc','def','ghi') if you want to update the labels

For a new plot:

figure

h1=plot([1 2],[15,30],'r','LineWidth',2);

hold on;

h2=plot([1 2],[5,35],'b','LineWidth',2);

h3=plot([1 2],[40 40],'--k','LineWidth',2);

ylim([5 50])

legend([h3 h2 h1],'3','2','1')

How to change the order of the legend in the Matlab version 2019b? (4)

4 Comments

Show 2 older commentsHide 2 older comments

Gopinath Karuppannan on 29 Oct 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1806303

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1806303

Thanks for the response.

My focus is editing the existing plots rather than for a new plot.

May i know the order of plot which we are giving is based upon the order of legend, Am i correct?

So,

Order - [1 2 3] ==> A B C

Order - [2 3 1] ==> B C A

Like above the order of legend has to be replaced right? But it doesn't behave as i mentioned.

The below figure is given in the order of [2 3 1]. I expected as [ B C A] but what i get is different.

How to change the order of the legend in the Matlab version 2019b? (6)

I hope you have understand the issue. Can you explain if my understanding on the legend order is wrong.

Thanks

Gopinath Karuppannan on 29 Oct 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1806363

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1806363

Hi

FYI: I have checked with different scenario in the order. Only two of them getting as in correct order.

Order - [1 2 3] ==> A B C

Order - [3 2 1] ==> C B A

Other than that, I am getting wrong. Refer the following image.

How to change the order of the legend in the Matlab version 2019b? (8)

Thanks in advance.

Dave B on 29 Oct 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1806838

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1806838

Open in MATLAB Online

  • check.fig

You're generally correct in your understanding but everything is upside down from what you described. That's a little confusing. I'd be surprised if that changed since 2019b, so I'm a little puzzled by your graphic above. In any case, you can be unambiguous about it by looking directly at the kids vector (see below) and you can certainly plot every combination (also below).

In general, I expect the first element of the kids vector that I defined above is the last thing plotted, and the last element of the kids vector is the first element plotted.

open('check.fig');

% I'm removing the semicolon so we can see what's in kids

% If you wanted to check these values programatically, you could use

% get(kids,'DisplayName') to get their 'names' or you can get some

% other aspect of them that you'd like to use to guide their order

kids=findobj(gca,'type','Line')

kids =

3×1 Line array: Line (C) Line (B) Line (A)

orig=gca;

Here's a look at all the combinations of orders:

figure;

t = tiledlayout(2,3);

p=perms([1 2 3]);

for i = 1:height(p)

h=copyobj(orig,t);

h.Layout.Tile=i;

kids=findobj(h,'type','Line');

legend(h,kids(p(i,:)));

title(h,sprintf('[%d %d %d]',p(i,:)))

end

How to change the order of the legend in the Matlab version 2019b? (10)

How to change the order of the legend in the Matlab version 2019b? (11)

Gopinath Karuppannan on 30 Oct 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1807693

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1574423-how-to-change-the-order-of-the-legend-in-the-matlab-version-2019b#comment_1807693

Okay Perfect. Thanks for the explanation. I understood it clearly. i can do further on this one.

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

  • matlab
  • matlab gui
  • function

Products

  • MATLAB

Release

R2019b

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 change the order of the legend in the Matlab version 2019b? (13)

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 change the order of the legend in the Matlab version 2019b? (2024)
Top Articles
More High Pay Offers for 50% Acceptance Rate: Is Doordash Lying?
Why DoorDash Acceptance Rates Don't Matter For Dashers
Spectrum Gdvr-2007
Dairy Queen Lobby Hours
Restaurer Triple Vitrage
Loves Employee Pay Stub
Txtvrfy Sheridan Wy
360 Training Alcohol Final Exam Answers
Minn Kota Paws
True Statement About A Crown Dependency Crossword
Aquatic Pets And Reptiles Photos
Unit 1 Lesson 5 Practice Problems Answer Key
The Cure Average Setlist
Tamilrockers Movies 2023 Download
Fdny Business
Prosser Dam Fish Count
Nail Salon Goodman Plaza
Kiddle Encyclopedia
Las 12 mejores subastas de carros en Los Ángeles, California - Gossip Vehiculos
Amazing deals for DKoldies on Goodshop!
Account Suspended
20 Different Cat Sounds and What They Mean
Sea To Dallas Google Flights
Ups Print Store Near Me
Lisas Stamp Studio
Marion City Wide Garage Sale 2023
PCM.daily - Discussion Forum: Classique du Grand Duché
Wics News Springfield Il
Danielle Ranslow Obituary
Hannaford Weekly Flyer Manchester Nh
D2L Brightspace Clc
The Creator Showtimes Near Baxter Avenue Theatres
Best Laundry Mat Near Me
Autopsy, Grave Rating, and Corpse Guide in Graveyard Keeper
Japanese Pokémon Cards vs English Pokémon Cards
2012 Street Glide Blue Book Value
October 31St Weather
Dr. John Mathews Jr., MD – Fairfax, VA | Internal Medicine on Doximity
Eastern New Mexico News Obituaries
Ksu Sturgis Library
South Bend Tribune Online
Wrigley Rooftops Promo Code
SF bay area cars & trucks "chevrolet 50" - craigslist
Mytime Maple Grove Hospital
Craigslist - Pets for Sale or Adoption in Hawley, PA
Lacy Soto Mechanic
Kutty Movie Net
Divinity: Original Sin II - How to Use the Conjurer Class
Rétrospective 2023 : une année culturelle de renaissances et de mutations
300 Fort Monroe Industrial Parkway Monroeville Oh
Predator revo radial owners
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 5283

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.