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

See Also

legend | Legend Properties

MATLAB Command

You clicked a link that corresponds to this MATLAB command:


Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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


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


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


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 to manually add legends 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 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 to manipulate 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 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 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.

Can you have 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: add a second (exatly equal) set of axes to the figure. make this axes invisible, so you don't see it later in the plot. add two "helping - lines", one solid and one dotted.

What is legend in graph? ›

A legend is an area of a diagram (graph) that identifies colors or patterns assigned to categories from a table.

How do you plot without a legend in MATLAB? ›

Below are two ways to remove a legend from a plot:
  • Use the "findobj" function to find the legend(s) and the "delete" function to delete them: % Find and delete the legend. lgd = findobj('type', 'legend') ...
  • If you do not want to delete the legend, you can turn off the legend's visibility: set(lgd(idx), 'visible', 'off')
Mar 26, 2016

What is the legend in MATLAB plot? ›

legend creates a legend with descriptive labels for each plotted data series. For the labels, the legend uses the text from the DisplayName properties of the data series. If the DisplayName property is empty, then the legend uses a label of the form 'dataN' .

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.

What is plot legend? ›

Plot legends give meaning to a visualization, assigning meaning to the various plot elements. We previously saw how to create a simple legend; here we'll take a look at customizing the placement and aesthetics of the legend in Matplotlib.

How to add legend in MATLAB scope? ›

On the Scope tab, the Configuration section allows you to modify the scope. The Legend button turns the legend on or off. When you show the legend, you can control which signals are shown.

How do you hide data from legend in MATLAB? ›

One can suppress a legend entry for a line object h by executing h. HandleVisibility='off' or h. Annotation.

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

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.

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 add legend to contour 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.

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5562

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.