Add Legend to Graph - MATLAB & Simulink - MathWorks France (2024)

Open Live Script

Legends are a useful way to label data series plotted on a graph. These examples show how to create a legend and make some common modifications, such as changing the location, setting the font size, and adding a title. You also can create a legend with multiple columns or create a legend for a subset of the plotted data.

Create Simple Legend

Create a figure with a line chart and a scatter chart. Add a legend with a description for each chart. Specify the legend labels as inputs to the legend function.

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

Add Legend to Graph- MATLAB & Simulink- MathWorks France (1)

Specify Labels Using DisplayName

Alternatively, you can specify the legend labels using the DisplayName property. Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend.

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

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. If you do not set the DisplayName property, then the legend uses a label of the form 'dataN'.

Add a scatter chart for 2017 data.

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

Add Legend to Graph- MATLAB & Simulink- MathWorks France (2)

Customize Legend Appearance

The legend function creates a Legend object. Legend objects have properties that you can use to customize the appearance of the legend, such as the Location, Orientation, FontSize, and Title properties. For a full list, see Legend Properties.

You can set properties in two ways:

  • Use name-value pairs in the legend command. In most cases, when you use name-value pairs, you must specify the labels in a cell array, such as legend({'label1','label2'},'FontSize',14).

  • Use the Legend object. You can return the Legend object as an output argument from the legend function, such as lgd = legend. Then, use lgd with dot notation to set properties, such as lgd.FontSize = 14.

Legend Location and Orientation

Specify the legend location and orientation by setting the Location and Orientation properties as name-value pairs. Set the location to one of the eight cardinal or intercardinal directions, in this case, 'northwest'. Set the orientation to 'vertical' (the default) or 'horizontal', as in this case. Specify the labels in a cell array.

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

Add Legend to Graph- MATLAB & Simulink- MathWorks France (3)

Legend Font Size and Title

Specify the legend font size and title by setting the FontSize and Title properties. Assign the Legend object to the variable lgd. Then, use lgd to change the properties using dot notation.

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';

Legend with Multiple Columns

Create a chart with six line plots. Add a legend with two columns by setting the NumColumns property to 2.

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;

Add Legend to Graph- MATLAB & Simulink- MathWorks France (5)

Include Subset of Charts in Legend

Combine two bar charts and a scatter chart. Create a legend that includes only the bar charts by specifying the Bar objects, b1 and b2, as the first input argument to the legend function. Specify the objects in a vector.

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

Add Legend to Graph- MATLAB & Simulink- MathWorks France (6)

See Also

legend | Legend Properties

Commande MATLAB

Vous avez cliqué sur un lien qui correspond à cette commande MATLAB:

 

Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.

Add Legend to Graph- MATLAB & Simulink- MathWorks France (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

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

Contact your local office

Add Legend to Graph
- MATLAB & Simulink
- MathWorks France (2024)

FAQs

How to add legend in Matlab Simulink? ›

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 I manually add a legend in Matlab? ›

Add Legend to Current Axes

Plot two lines and add a legend to the current axes. Specify the legend labels as input arguments to the legend function. If you add or delete a data series from the axes, the legend updates accordingly.

How do you add a legend title to a plot 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.

How to write a title in Matlab? ›

Create a plot. Then create a title and a subtitle by calling the title function with two character vectors as arguments. Use the 'Color' name-value pair argument to customize the color for both lines of text. Specify two return arguments to store the text objects for the title and subtitle.

How to change legend in simulink scope? ›

2 Answers. 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.

What is a legend on a graph? ›

The legend of a graph reflects the data displayed in the graph's Y-axis, also called the graph series. This is the data that comes from the columns of the corresponding grid report, and usually represents metrics. A graph legend generally appears as a box to the right or left of your graph.

How do you add 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 to put text on a graph in MATLAB? ›

To add text to one point, specify x and y as scalars. To add text to multiple points, specify x and y as vectors with equal length. text( x , y , z , txt ) positions the text in 3-D coordinates. text(___, Name,Value ) specifies Text object properties using one or more name-value pairs.

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

legend() in Python. The attribute Loc in legend() is used to specify the location of the legend. The default value of loc is loc= “best” (upper left). The strings 'upper left', 'upper right', 'lower left', and 'lower right' place the legend at the corresponding corner of the axes/figure.

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 one legend to subplot MATLAB? ›

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 to define legend in MATLAB? ›

h = legend(...) returns a handle to the legend, which is an Axes graphics object. legend associates strings with the objects in the Axes in the same order that they are listed in the Axes Children property. By default, the legend annotates the current Axes. MATLAB displays only one legend per Axes.

What is the title of the graph? ›

Graph Title: The title appears at the top of the graph and should describe the graph. Axis Labels: The labels that appear along the x and y-axes describing what is being measured.

How to add labels in Simulink? ›

To add a label to a project file, in the Files view, select the file. Then, drag the desired label from the Labels panel at the bottom left of the project into the Label Editor panel for the selected file. The Label Editor panel is located at the bottom right of the Files view.

What is the legend function in MATLAB? ›

legend associates strings with the objects in the Axes in the same order that they are listed in the Axes Children property. By default, the legend annotates the current Axes. MATLAB displays only one legend per Axes. legend positions the legend based on a variety of factors, such as what objects the legend obscures.

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5566

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.