Hinzufügen einer Legende zu einem Diagramm - MATLAB & Simulink - MathWorks Deutschland (2024)

Live Script öffnen

Mit Legenden können auf einem Diagramm eingezeichnete Datenserien beschriftet werden. In diesen Beispielen ist dargestellt, wie sich eine Legende erstellen und einige übliche Änderungen vornehmen lassen, wie Ändern der Position, Einstellen der Schriftgröße und Hinzufügen eines Titels. Sie können zudem eine Legende mit mehreren Spalten oder eine Legende für eine Teilmenge der im Diagramm dargestellten Daten erstellen.

Erstellen einer einfachen Legende

Erstellen Sie eine Abbildung mit einem Liniendiagramm und einem Streudiagramm. Fügen Sie jedem Diagramm eine Legende mit einer Beschreibung hinzu. Legen Sie die Beschriftungen der Legende als Eingaben für die legend-Funktion fest.

figurex1 = linspace(0,5);y1 = sin(x1/2);plot(x1,y1)hold onx2 = [0 1 2 3 4 5];y2 = [0.2 0.3 0.6 1 0.7 0.6];scatter(x2,y2,'filled')hold offlegend('sin(x/2)','2016')

Hinzufügen einer Legende zu einem Diagramm- MATLAB & Simulink- MathWorks Deutschland (1)

Festlegen von Beschriftungen mittels DisplayName

Alternativ können Sie die Legendenbeschriftungen mithilfe der Eigenschaft DisplayName festlegen. Legen Sie die Eigenschaft DisplayName als Namenswert-Paar fest, wenn Sie die Diagrammfunktionen aufrufen. Rufen Sie daraufhin den Befehl legend auf, um die Legende zu erstellen.

x1 = linspace(0,5);y1 = sin(x1/2);plot(x1,y1,'DisplayName','sin(x/2)')hold onx2 = [0 1 2 3 4 5];y2 = [0.2 0.3 0.6 1 0.7 0.6];scatter(x2,y2,'filled','DisplayName','2016')legend

Legenden werden automatisch aktualisiert, wenn Sie eine Datenserie hinzufügen oder löschen. Wenn Sie den Achsen weitere Daten hinzufügen, können Sie die Beschriftungen über die Eigenschaft DisplayName festlegen. Wenn Sie die Eigenschaft DisplayName nicht festlegen, verwendet die Legende eine Beschriftung der Form 'dataN'.

Fügen Sie ein Streudiagramm für 2017-Daten hinzu.

x3 = [0 1 2 3 4 5];y3 = [0.1 0.4 0.6 0.9 0.8 0.7];scatter(x3,y3,'filled','DisplayName','2017')drawnowhold off

Hinzufügen einer Legende zu einem Diagramm- MATLAB & Simulink- MathWorks Deutschland (2)

Anpassen der Legendendarstellung

Mit der Funktion legend wird ein Legend-Objekt erstellt. Legend-Objekte weisen Eigenschaften auf, mit denen Sie das Erscheinungsbild der Legende anpassen können, darunter Location, Orientation, FontSize und Title. Eine vollständige Liste finden Sie unter Legend Properties.

Es gibt zwei Methoden zum Festlegen von Eigenschaften:

  • Sie können die Namenswert-Paare mit dem legend-Befehl verwenden. Wenn Sie Namenswert-Paare verwenden, müssen Sie in den meisten Fällen die Beschriftungen in einem Zellen-Array wie z.B. legend({'label1','label2'},'FontSize',14) festlegen.

  • Sie können das Objekt Legend verwenden. Sie können das Legend-Objekt als Ausgabeargument der Funktion legend ausgeben, wie z.B. lgd = legend. Daraufhin können Sie lgd mit Punktnotation verwenden, um Eigenschaften wie lgd.FontSize = 14 festzulegen.

Position und Ausrichtung der Legende

Legen Sie Position und Ausrichtung der Legende durch das Festlegen der Eigenschaften Location und Orientation mittels Namenswert-Paaren fest. Sie können die Position auf eine der acht Himmels- bzw. Nebenhimmelsrichtungen setzen, in diesem Fall 'northwest'. Sie können die Ausrichtung auf 'vertical' (Standard) oder wie in diesem Fall auf 'horizontal' festlegen. Legen Sie die Beschriftungen in einem Zellen-Array fest.

x1 = linspace(0,5);y1 = sin(x1/2);plot(x1,y1)hold onx2 = [0 1 2 3 4 5];y2 = [0.2 0.3 0.6 1 0.7 0.6];scatter(x2,y2,'filled')hold offlegend({'sin(x/2)','2016'},'Location','northwest','Orientation','horizontal')

Hinzufügen einer Legende zu einem Diagramm- MATLAB & Simulink- MathWorks Deutschland (3)

Schriftgröße und Titel der Legende

Legen Sie die Eigenschaften FontSize und Title fest, um Schriftgröße und Titel der Legende anzugeben. Weisen Sie das Legend-Objekt der Variablen lgd zu. Daraufhin können Sie lgd verwenden, um die Eigenschaften mithilfe von Punktnotation zu verändern.

x1 = linspace(0,5);y1 = sin(x1/2);plot(x1,y1,'DisplayName','sin(x/2)')hold onx2 = [0 1 2 3 4 5];y2 = [0.2 0.3 0.6 1 0.7 0.6];scatter(x2,y2,'filled','DisplayName','2016')hold offlgd = legend;lgd.FontSize = 14;lgd.Title.String = '2016 Data';

Hinzufügen einer Legende zu einem Diagramm- MATLAB & Simulink- MathWorks Deutschland (4)

Legende mit mehreren Spalten

Erstellen Sie ein Diagramm mit sechs Liniendiagrammen. Fügen Sie eine Legende mit zwei Spalten hinzu, indem Sie die Eigenschaft NumColumns auf 2 setzen.

x = linspace(0,10);y1 = sin(x);y2 = sin(0.9*x);y3 = sin(0.8*x);y4 = sin(0.7*x);y5 = sin(0.6*x);y6 = sin(0.5*x);plot(x,y1,'DisplayName','sin(x)')hold onplot(x,y2,'DisplayName','sin(0.9x)')plot(x,y3,'DisplayName','sin(0.8x)')plot(x,y4,'DisplayName','sin(0.7x)')plot(x,y5,'DisplayName','sin(0.6x)')plot(x,y6,'DisplayName','sin(0.5x)')hold offlgd = legend;lgd.NumColumns = 2;

Hinzufügen einer Legende zu einem Diagramm- MATLAB & Simulink- MathWorks Deutschland (5)

Aufnehmen einer Teilmenge des Diagramms in die Legende

Kombinieren Sie zwei Balkendiagramme und ein Streudiagramm. Erstellen Sie eine Legende, die nur die Balkendiagramme umfasst, indem Sie die Bar-Objekte b1 und b2 als erstes Eingabeargument für die Funktion legend angeben. Geben Sie die Objekte in einem Vektor an.

x = [1 2 3 4 5];y1 = [.2 .4 .6 .4 .2];b1 = bar(x,y1);hold on y2 = [.1 .3 .5 .3 .1];b2 = bar(x,y2,'BarWidth',0.5);y3 = [.2 .4 .6 .4 .2];s = scatter(x,y3,'filled');hold offlegend([b1 b2],'Bar Chart 1','Bar Chart 2')

Hinzufügen einer Legende zu einem Diagramm- MATLAB & Simulink- MathWorks Deutschland (6)

Siehe auch

legend | Legend Properties

MATLAB-Befehl

Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:

 

Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.

Hinzufügen einer Legende zu einem Diagramm- MATLAB & Simulink- MathWorks Deutschland (7)

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

Contact your local office

Hinzufügen einer Legende zu einem Diagramm
- MATLAB & Simulink
- MathWorks Deutschland (2024)

FAQs

How to add legend to matlab plot? ›

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 to plot Simulink model in Matlab? ›

From the Simulink Editor, right-click the logging badge for a signal, and select Properties to open the Instrumentation Properties for the signal. Then, click the graphic representation of the Line. From the Instrumentation Properties, you can also select subplots where you want to plot the signal.

How to title a 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.

How to put two legends in Matlab? ›

As far as I know, you can only have one legend-window for one set of axes in MATLAB, so the idea is:
  1. add a second (exatly equal) set of axes to the figure.
  2. make this axes invisible, so you don't see it later in the plot.
  3. add two "helping - lines", one solid and one dotted.
  4. make these helping - lines also invisible.
Nov 19, 2018

How to write a legend for a graph? ›

4 Features of a Good Figure Legend:
  1. Title: A brief title that applies to the entire figure, including all panels. ...
  2. Materials and methods: A description of the techniques used. ...
  3. Results: A statement of the results that can be gleaned from the particular figure. ...
  4. Definitions: An explanation of features in the figure.
Dec 29, 2014

How to format a legend in MATLAB? ›

Specify Legend Font Size and Color

Create a legend and assign the Legend object to the variable lgd . Set the FontSize and TextColor properties using name-value arguments. rdm = rand(4); plot(rdm) lgd = legend({'Line 1','Line 2','Line 3','Line 4'},... 'FontSize',12,'TextColor','blue');

What is an example of a legend in MATLAB? ›

Add a legend to a graph showing a sine and cosine function: x = - pi:pi/20:pi; plot(x,cos(x),' - r',x,sin(x),' - . b') h = legend('cos','sin',0); In this example, the plot command specifies a solid, red line ( ' - r' ) for the cosine function and a dash-dot, blue line ( ' - .

How do you name a legend in Simulink scope? ›

In the Scope window, select View>Legend option to view different signals and you can easily change the name of the signals in legend window. If you double click on a signal, a small text entry field will appear that allows you to name it. The name of the signal will then appear in the scope's ledgend.

How do you add a legend to a contour plot in MATLAB? ›

clegendm( C , h ) adds a legend specifying the contour line heights, C , to the current map contour plot, h . clegendm( C , h , loc ) places the legend in a specified location. clegendm(___, unitstr ) appends a string unitstr to each entry in the legend.

How to set legend position in MATLAB? ›

The Matlab help for legend clearly states:
  1. LEGEND(...,' Location',LOC) adds a legend in the specified.
  2. location, LOC, with respect to the axes. LOC may be either a.
  3. 1x4 position vector or one of the following strings:
  4. 'North' inside plot box near top.
  5. 'South' inside bottom.
Jul 27, 2011

How to add legend in MATLAB scope? ›

On the Scope tab, the Configuration section allows you to modify the scope.
  1. The Legend button turns the legend on or off. ...
  2. The Settings button opens the settings window which allows you to customize the data, axes, display settings, labels, and color settings.

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 do you put a legend inside a plot? ›

You can place the legend literally anywhere. 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 to add legend to plot in matplotlib? ›

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. x = np. linspace(0, 10, 1000) fig, ax = plt. ...
  3. ax. legend(loc='upper left', frameon=False) fig.
  4. ax. ...
  5. ax. ...
  6. In [7]: ...
  7. In [8]:

Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 6545

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.