How can I hold the previous "legend" on a plot? (2024)

293 views (last 30 days)

Show older comments

sadel on 14 Jun 2011

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot

Commented: Walter Roberson on 28 Sep 2021

Accepted Answer: sadel

I use the "hold" function to hold the previous plots. But how can I hold the previous legends, too?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

sadel on 17 Jun 2011

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#answer_13360

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#answer_13360

Open in MATLAB Online

here is the answer that I was looking for:

x=0:.01:10;

plot(x, sin(x), 'DisplayName','sin');

legend('-DynamicLegend');

hold all; % add new plot lines on top of previous ones

plot(x, cos(x), 'DisplayName','cos');

6 Comments

Show 4 older commentsHide 4 older comments

Wycliff Dembe on 7 Feb 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_794519

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_794519

This got the job done. Thanks

Walter Roberson on 7 Feb 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_794528

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_794528

Open in MATLAB Online

In sufficiently new MATLAB, if you called legend without passing in a list of graphics objects, then MATLAB will automatically add new objects created after that point to the legend. Occasionally you may need to

legend show

But back when the question was originally asked, the method shown here was best.

Michael Sonnenberg on 24 Apr 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_834080

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_834080

Thanks for the question and answer.

Paul Gwambe on 17 Sep 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1013494

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1013494

Thank you, it worked for me.

Harshia Mathur on 28 Sep 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1758229

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1758229

worked for me as well, Do you know anyway to customise this? I want to append a string, otherwise all it's saying is data1 , data2 etc..

Walter Roberson on 28 Sep 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1758359

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1758359

Open in MATLAB Online

@Harshia Mathur example

x=0:.01:10;

for alpha = [1 5 9]

plot(x, nthroot(sin(x).^alpha,7), 'DisplayName', "sin^{" + (alpha/7) + "}");

hold on;

end

hold off

ylim auto

legend show

How can I hold the previous "legend" on a plot? (9)

Sign in to comment.

More Answers (3)

Gerd on 14 Jun 2011

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#answer_13022

Open in MATLAB Online

For example I you would like to show the sin and cos you would use

t=[0:0.1:10]

x=sin(t)

y=cos(t)

plot(t,x)

hold

plot(t,y,'r')

legend('sin','cos')

In your case you have to build the legendstring with the two different "strings" you want to show.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Gerd on 14 Jun 2011

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#answer_13008

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#answer_13008

Open in MATLAB Online

Hi sadel, to get the current legend use

% you have to have a legend on the current plot

get(legend(gca),'String'); % get legend from current axes.

% Now you can set the legend of the new plot

Gerd

5 Comments

Show 3 older commentsHide 3 older comments

sadel on 14 Jun 2011

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20656

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20656

Hi Gerd and thank you for your reply!

My code is on a button and is something like this:

if get(handles.holdaxes,'value')==1

hold on

get(legend(gca),'String');

else

hold off

end

switch get(val1)

case 1

string='vvsdv'

plot 1

legend(string)

case 2

string='vdsvsdvs'

plot 2

legend(string)

end

when I click the button for second time, my program just change the string of the legend but I want to have 2 lines legend. How can I do this?

Gerd on 14 Jun 2011

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20666

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20666

Hi sadel, what do you mean by having a 2 lines legend?

Do you want to have 2 plots with 2 legend?

Gerd

sadel on 14 Jun 2011

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20667

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_20667

yes

Rajani Metri on 5 Dec 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1185118

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1185118

Hello,

but legend holding only text. It is showing legend indicators as lines, instead of asteric lines, which I used. It is not holding the marking points of plots. For holding Marker points of respective plots, what to do?

Thank You.

Walter Roberson on 5 Dec 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1185688

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#comment_1185688

Which MATLAB version are you using? And please explain what you are trying to do in more detail?

Sign in to comment.

Andrea Cavallini on 6 Feb 2018

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#answer_303733

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/9434-how-can-i-hold-the-previous-legend-on-a-plot#answer_303733

Quick and dirty... at last (before hold off), draw something out of the scope of the plot, but with the right legend symbols. That will do

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

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

  • hold legend

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 can I hold the previous "legend" on a plot? (18)

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 can I hold the previous "legend" on a plot? (2024)

FAQs

How do you put a legend inside a plot? ›

Control legend position with legend.

To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.

How can we customize plot legend? ›

Customizing Plot Legends
  1. import matplotlib.pyplot as plt plt. style. ...
  2. %matplotlib inline import numpy as np.
  3. x = np. linspace(0, 10, 1000) fig, ax = plt. ...
  4. ax. legend(loc='upper left', frameon=False) fig.
  5. ax. legend(frameon=False, loc='lower center', ncol=2) fig.
  6. ax. ...
  7. In [7]: ...
  8. In [8]:

When should you include a legend on a plot? ›

Answer. If your graph has more than one line, then you should always include a legend. Graphs should try to be self-explanatory, and provide enough information that someone unfamiliar with it, can look at it and understand what the data represents and what each line is describing.

How do you move the legend out of a plot in Seaborn? ›

To move the legend outside the plot, we'll use the bbox_to_anchor and loc parameters of the legend function. The bbox_to_anchor parameter specifies the legend's position. The tuple (1.05, 1) positions the legend just outside the plot's right edge. The loc parameter determines where the legend's anchor point should be.

How do you put a legend outside a figure? ›

Figure legends

Sometimes it makes more sense to place a legend relative to the (sub)figure rather than individual Axes. By using constrained layout and specifying "outside" at the beginning of the loc keyword argument, the legend is drawn outside the Axes on the (sub)figure.

How do you insert a legend? ›

Click the chart, and then click the Chart Design tab. Click Add Chart Element > Legend. To change the position of the legend, choose Right, Top, Left, or Bottom. To change the format of the legend, click More Legend Options, and then make the format changes that you want.

How do you modify a legend? ›

Edit legend entries on the worksheet
  1. On the worksheet, click the cell that contains the name of the data series that appears as an entry in the chart legend.
  2. Type the new name, and then press ENTER. The new name automatically appears in the legend on the chart.

How do you structure a legend? ›

How to Write a Legend: Step-by-Step
  1. Set the story in today's world.
  2. Change or add plot details.
  3. Change a few main events.
  4. Change the gender of the hero or heroine.
  5. Change the point of view (example: Tell the legend of St. ...
  6. Write a sequel.
  7. Write a prequel.
  8. Develop an existing legend into a readers' theatre script.

Where is the best place to put a legend? ›

Legend Placement

Legends should be placed below or to the right to a data visualization, depending on the type of data being labeled and the available space surrounding the data visualization. When a large set of values exist ensure the legend is placed on the right of the chart.

Does a legend need a title? ›

The same principle applies when adding text to your legend, such as a legend title. Legend titles should be used to add context and explain your map. Don't title your legend “legend”—your reader will know it is a legend. If there's no better title then "legend", it doesn't need a title at all.

How should a legend look? ›

Generally the figure legend should include:
  1. A title.
  2. The materials & methods involved with the presented figure.
  3. Results, though this isn't always applicable.
  4. Any other miscellaneous details such as explaining abbreviations or image scale.
May 13, 2021

How do you move the legend in a plot in Python? ›

You can do this using the bbox_to_anchor argument. This argument takes a tuple of two or four numbers, which represent the coordinates for the legend's new location. In this case, the first number in the tuple moves the legend along the x-axis, and the second number moves it along the y-axis.

How do I rearrange the legend on a chart? ›

Under Chart Tools, on the Design tab, in the Data group, click Select Data. In the Select Data Source dialog box, in the Legend Entries (Series) box, click the data series that you want to change the order of. Click the Move Up or Move Down arrows to move the data series to the position that you want.

How do we change the legend properties in a chart? ›

Customize the Legend in a chart
  1. Select the chart object.
  2. In the Properties pane, under Chart Annotations, double-click the Legend property.
  3. To show the legend on the chart, select the Show Legend check box.
  4. To choose a preset legend position, click Preset and click a position from the diagram.

How do you put a legend inside a bar graph? ›

Add a chart legend
  1. Click the chart.
  2. Click Chart Elements. next to the table.
  3. Select the Legend check box. The chart now has a visible legend.

How do you add a legend to a count plot? ›

The easiest is to use hue= with the same variable as x= . You'll need to set dodge=False as by default a position is reserved for each x - hue combination. Note that when you aren't using hue , no legend is added as the names and colors are given by the x tick labels.

References

Top Articles
Journal articles: 'Boot Camp (Computer file)' – Grafiati
What Is an Image Consultant & What Do They Do?
San Angelo, Texas: eine Oase für Kunstliebhaber
Bleak Faith: Forsaken – im Test (PS5)
How Many Cc's Is A 96 Cubic Inch Engine
Skip The Games Norfolk Virginia
Pbr Wisconsin Baseball
Joe Gorga Zodiac Sign
biBERK Business Insurance Provides Essential Insights on Liquor Store Risk Management and Insurance Considerations
Craigslistdaytona
Uvalde Topic
Ktbs Payroll Login
Insidekp.kp.org Hrconnect
Illinois Gun Shows 2022
Louisiana Sportsman Classifieds Guns
Aldi Sign In Careers
Bx11
Abortion Bans Have Delayed Emergency Medical Care. In Georgia, Experts Say This Mother’s Death Was Preventable.
Trac Cbna
Is The Yankees Game Postponed Tonight
Hobby Stores Near Me Now
Vegas7Games.com
Imouto Wa Gal Kawaii - Episode 2
6 Most Trusted Pheromone perfumes of 2024 for Winning Over Women
Gillette Craigslist
Lacey Costco Gas Price
Frank Vascellaro
Martins Point Patient Portal
Craigslist Texas Killeen
Haunted Mansion Showtimes Near Cinemark Tinseltown Usa And Imax
Tamilrockers Movies 2023 Download
Car Crash On 5 Freeway Today
Federal Student Aid
Rocketpult Infinite Fuel
The Boogeyman Showtimes Near Surf Cinemas
Hannibal Mo Craigslist Pets
Raisya Crow on LinkedIn: Breckie Hill Shower Video viral Cucumber Leaks VIDEO Click to watch full…
The best Verizon phones for 2024
9781644854013
The Banshees Of Inisherin Showtimes Near Reading Cinemas Town Square
Hometown Pizza Sheridan Menu
Flags Half Staff Today Wisconsin
Simnet Jwu
Carroll White Remc Outage Map
Courtney Roberson Rob Dyrdek
Busted Newspaper Mcpherson Kansas
Keci News
Ihop Deliver
Model Center Jasmin
Craigslist Pets Lewiston Idaho
Charlotte North Carolina Craigslist Pets
Primary Care in Nashville & Southern KY | Tristar Medical Group
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 5657

Rating: 5 / 5 (80 voted)

Reviews: 87% 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.