How to display only one legend for a figure with multiple plots? (2024)

161 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Armel am 10 Jun. 2013

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots

Kommentiert: Adam Danz am 16 Sep. 2022

Hello everyone,

I have two questions:

1) How can I display one unique legend for a figure with multiple plots?

2) Let assume 5,4,3, 2 and 1 as code for "very high", "High", "Moderate", "Low", and "Very Low" the values of my variable of interest. How can I display these characters strings instead of the value 5,4,3,2, and 1 in the legend?

Any thoughts for a code will be very much appreciated.

Armel

0 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Antworten (5)

Camilo Malagon Nieto am 24 Apr. 2018

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_316940

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_316940

I got a better solution that the one with b = get(gca,'Children')

From MatLab Documentation under Legend>Specify Charts to Include in Legend

x = linspace(0,3*pi); y1 = sin(x); p1 = plot(x,y1);

hold on y2 = sin(x - pi/4); p2 = plot(x,y2);

y3 = sin(x - pi/2); p3 = plot(x,y3); hold off

legend([p1 p3],'First','Third')

2 Kommentare

Keine anzeigenKeine ausblenden

hamid rafiei am 7 Sep. 2022

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_2350620

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_2350620

Dear

thanks for your answer

if I have a figure that saved as " .fig", how can I display one unique legend for a figure with multiple plots?

Thanks again

Adam Danz am 16 Sep. 2022

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_2367025

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_2367025

See this answer below.

Melden Sie sich an, um zu kommentieren.

Raghu S am 30 Aug. 2018

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_334793

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_334793

Here is the answer: menation these line inside legend command 'orientation','horizontal'

example: legend('orientation','horizontal','name1','name2','name3')

some example:

____________Mutiple legend:_______________

t=0:0.001:1;

sig1=2*sin(2*pi*10*t);

sig2=2*cos(2*pi*10*t);

subplot 212;plot(sig1);hold on;plot(sig2);legend('sin','cos')

subplot 211;plot(sig1);hold on;plot(sig2);legend('sin','cos')

____________Single legend___________________

legend('Location','bestoutside','orientation','horizontal','sin','cos')

One can move anywhere from the figure editor.

Attached one more example.

0 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Angus am 10 Jun. 2013

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_88368

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_88368

"legend('string1','string2',...) displays a legend in the current axes using the specified strings to label each set of data." ~ Matlab docs.

This is the simplest way of assigning strings to the legend. The docs show other uses.

When you say you have multiple plots, do you mean multiple axis or just multiple data plots within one axis? If you have multiple axis then you can create a unique legend that specifies what you want by using the second method from the docs:

"legend(h,'string1','string2',...) displays a legend on the plot containing the objects identified by the handles in the vector h and uses the specified strings to label the corresponding graphics object (line, barseries, etc.)."

3 Kommentare

1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154651

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154651

In MATLAB Online öffnen

Here is a crude example that I think illustrates the method.

Basically you need to get the handles for whichever objects you want in your legend, this can by done multiple ways and what I used is definitely not the best, just easy this time; you may want to use findobj(...) to get the handles if need be. Then choose a location for your legend and pass it the object handles and assign the strings. It is also possible to tidy things up by preparing the strings in an array, there are options for these things in legend().

x = [1:100];

y1 = sin(x);

y2 = 2*sin(x./2);

y3 = [1:100];

y4 = [100:-1:1];

subplot(121)

plot(x,y1,'b',x,y2,'r')

a = get(gca,'Children')

subplot(122)

plot(x,y3,'g',x,y4,'k')

b = get(gca,'Children')

h = [b;a]

legend(h,'decreasing','increasing','sine2','sine1','location','eastoutside')

Armel am 12 Jun. 2013

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154659

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154659

Bearbeitet: Armel am 12 Jun. 2013

In fact, here is a similar image that I would like to produce https://docs.google.com/file/d/0BwjZP-sfazLMS3JTVVJUTkdzNEE/edit?usp=sharing 2 (or many) images with only one legend but the images are raster data.

Angus am 12 Jun. 2013

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154664

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154664

Yes, I saw that before, I'm not sure if raster data changes the method used. I'm not sure how raster data is dealt with in matlab, I dont use it. If you can get it to plot as in my example (convert it to a vector?) then this method should work for you.

Have I answered the original question about legends and multiple plots? Sorry I cant be much help with raster data use.

Cheers, Angus.

Melden Sie sich an, um zu kommentieren.

Armel am 11 Jun. 2013

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_88384

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_88384

Hi Angus,

Thanks for your reply.

Is there a way to upload a similar figure so you will be able to understand what I mean?

Armel

P.S I'm new to this community

1 Kommentar

-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Angus am 11 Jun. 2013

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154356

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#comment_154356

Hey, I am actually still new here as well. I am not sure if there is a standard method for linking images but I have seen some people upload via other websites like TinyPic.

Cheers, Angus.

Melden Sie sich an, um zu kommentieren.

Adam Danz am 29 Sep. 2020

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_1054970

  • Verknüpfen

    Direkter Link zu dieser Antwort

    https://de.mathworks.com/matlabcentral/answers/78635-how-to-display-only-one-legend-for-a-figure-with-multiple-plots#answer_1054970

Verschoben: Adam Danz am 16 Sep. 2022

Update

Starting in Matlab r2020b, legends can be positioned relative to figure edges and can contain graphics objects from different subplots created by TiledLayout.

Examples:

  • Example of a global legend in a 2x2 tiled layout
  • Example of a global legend in a 1x4 tiled layout
  • Example of a global legend in a 1x4 subplot
0 Kommentare

-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Kategorien

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Mehr zu Legend finden Sie in Help Center und File Exchange

Tags

  • plot
  • legend

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


Translated by How to display only one legend for a figure with multiple plots? (13)

How to display only one legend for a figure with multiple plots? (14)

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europa

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

Asien-Pazifik

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Kontakt zu Ihrer lokalen Niederlassung

How to display only one legend for a figure with multiple plots? (2024)

FAQs

How to make one legend for all subplots? ›

There is no built-in way to create an "overall legend". But it is perfectly fine to use a legend associated with a subplot as an overall legend. You will have to play with the legend's position to achieve the desired look. The easiest way to do it is manually, by dragging the legend inside the figure.

How do you write a figure legend for multiple graphs? ›

Include Specific Details: Provide Enough Information for Readers to Interpret the Figure Accurately
  1. Clarify abbreviations.
  2. Describe symbols.
  3. Detail axis references These steps ensure everyone—regardless of expertise level—can follow along smoothly.
Feb 9, 2024

How do I make multiple Matplotlib plots in one figure? ›

Matplotlib, a popular Python library for data visualization, provides an easy way to create multiple plots on the same figure using the `add_subplot()` method. The `add_subplot()` method takes three arguments: the number of rows, the number of columns, and the index of the plot.

How do you plot multiple graphs with legend in Python? ›

To plot multiple graphs on one plot, follow these steps.
  1. Install and import the matplotlib and NumPy library. ...
  2. Create an array of time using the np. ...
  3. Now plot the graph one as plt.subplot(121) plt.plot(t, 'r- -') plt.xlabel('Plot 1)
  4. Similarly, plot graph 2 as … ...
  5. Now show both the graph in one plot as…
Oct 10, 2022

How do I add a legend to a specific plot in Matlab? ›

Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend. Legends automatically update when you add or delete a data series. If you add more data to the axes, use the DisplayName property to specify the labels.

How do you set a legend outside a subplot? ›

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.

Can one figure have multiple graphs? ›

A subplot () function is a wrapper function which allows the programmer to plot more than one graph in a single figure by just calling it once.

What is the difference between table legend and figure legend? ›

Table Legends: These are captions for tables with a single phrase or sentence. Figure Legends: These are captions for figures which give overall information about the figure represented and are usually longer than table legends.

Which method provides a way to plot multiple plots on a single figure? ›

subplots() method provides a way to plot multiple plots on a single figure.

How to show multiple plots in one figure matlab? ›

You can display multiple axes in a single figure by using the tiledlayout function. This function creates a tiled chart layout containing an invisible grid of tiles over the entire figure. Each tile can contain an axes for displaying a plot.

How to plot in different subplots in Matplotlib? ›

To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with the objects Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.

How to set legend in subplots Matplotlib? ›

Matplotlib legends in subplot
  1. Using numpy, create points for x, y1, y2 and y3.
  2. Create a figure and a set of subplots, using the subplots() method, considering 3 subplots.
  3. Plot the curve on all the subplots(3), with different labels, colors. ...
  4. To activate label for each curve, use the legend() method.
Oct 26, 2023

How do you write a legend in a plot in Python? ›

The simplest legend can be created with the plt.legend() command, which automatically creates a legend for any labeled plot elements:
  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. ...
  5. ax. ...
  6. ax. ...
  7. In [7]: ...
  8. In [8]:

How do I add two different legends in Matplotlib? ›

To add a second legend, we need to use the ax. legend() method to create the first legend, and then manually add a second legend using the plt. gca(). add_artist() method.

How to add legend to each subplot in Matplotlib? ›

Matplotlib legends in subplot
  1. Using numpy, create points for x, y1, y2 and y3.
  2. Create a figure and a set of subplots, using the subplots() method, considering 3 subplots.
  3. Plot the curve on all the subplots(3), with different labels, colors. ...
  4. To activate label for each curve, use the legend() method.
Oct 26, 2023

How to set a single main title for all the subplots in Matplotlib? ›

The multiple subplots are defined by a collection of different plotting graphs. The Matplotlib is name of the module that provides animated and interactive visualization in Python. In Python, we have some built-in function named suptitle() that can be used to Set a Single Main Title for All the Subplots in Matplotlib.

How to set a single main title above all the subplots matlab? ›

sgtitle( txt ) adds a title above the grid of subplots in the current figure. If a figure does not it exist, then this command creates one. sgtitle( target , txt ) adds the title to the subplot grid in the specified figure, panel, or tab, instead of the current figure.

Can a map have more than one legend? ›

On the "map" visual you are only able to add one legend which is a color legend. Often times you need two legends.

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6077

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.