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:

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 (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 you add a legend to a plot in MATLAB Python? ›

To create a legend in Matplotlib, you can use the legend() function after plotting your data. This function adds a legend to your plot, which helps to identify different data series.

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

Use lgd to query and set properties of the legend after it is created. For a list of properties, see Legend Properties. legend( vsbl ) controls the visibility of the legend, where vsbl is 'hide' , 'show' , or 'toggle' . legend('off') deletes the legend.

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.

What is the legend function in Matlab? ›

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

What is legend in 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 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 do I change the name of a legend in Simulink? ›

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 multiple legends in MATLAB? ›

Direct link to this answer
  1. t = 0:0.1:5;
  2. y = sin(t);
  3. z = cos(t);
  4. figure; hold on;
  5. for i = 1:2.
  6. p1(i) = plot(t, y,'r');
  7. p2(i) = plot(t, z,'b');
  8. end.
Feb 22, 2023

How to add a legend title in MATLAB? ›

Answers (1)
  1. legh = legend('line y = x');
  2. t = get(legh, 'Title');
  3. set(t, 'String', 'hello')
Dec 5, 2023

Which command is used to add a legend to a MATLAB plot? ›

set( gca, 'fontsize', 16 ); A legend can be added with the following command. legend( 'Line 1', 'Line 2', 'Line 3' ); In addition to specifying the labels as individual character strings, it is often convenient to collect the strings in a cell array.

What is the limit of legend in MATLAB? ›

Accepted Answer

Legends are currently limited to no more than 50 entries. Usually in plots with more than 50 features, the plot is so cluttered and the legend is so large that it is more advisable to select just a few key items to display in the legend.

How do you hide entries in MATLAB legend? ›

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

How to add legend in data inspector Simulink? ›

Set Legend Position in Simulation Data Inspector

Open the Simulation Data Inspector. Select signals from the table on the left to view them in the Simulation Data Inspector. By default, the legend is located outside of the subplot at the top-left. Use the Simulink.

How to name 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 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 do you give a legend a title in MATLAB? ›

Answers (1)
  1. legh = legend('line y = x');
  2. t = get(legh, 'Title');
  3. set(t, 'String', 'hello')
Dec 5, 2023

References

Top Articles
21 Low Carb High Protein Vegetarian Recipes
Tex Mex Cheesy Chicken Soup Recipe
Dragon Age Inquisition War Table Operations and Missions Guide
Loves Employee Pay Stub
Bellinghamcraigslist
Tx Rrc Drilling Permit Query
Palace Pizza Joplin
The Many Faces of the Craigslist Killer
Strange World Showtimes Near Cmx Downtown At The Gardens 16
Simple Steamed Purple Sweet Potatoes
Cool Math Games Bucketball
Vcuapi
Bowie Tx Craigslist
Shannon Dacombe
Panorama Charter Portal
DBZ Dokkan Battle Full-Power Tier List [All Cards Ranked]
Vandymania Com Forums
Bible Gateway passage: Revelation 3 - New Living Translation
Why do rebates take so long to process?
All Obituaries | Verkuilen-Van Deurzen Family Funeral Home | Little Chute WI funeral home and cremation
683 Job Calls
Helpers Needed At Once Bug Fables
Synergy Grand Rapids Public Schools
Churchill Downs Racing Entries
Top 20 scariest Roblox games
8002905511
Marlene2995 Pagina Azul
Rural King Credit Card Minimum Credit Score
manhattan cars & trucks - by owner - craigslist
Neteller Kasiinod
Astro Seek Asteroid Chart
The Monitor Recent Obituaries: All Of The Monitor's Recent Obituaries
Poe T4 Aisling
Bursar.okstate.edu
Davita Salary
Www Craigslist Com Shreveport Louisiana
Appleton Post Crescent Today's Obituaries
Rise Meadville Reviews
The Mad Merchant Wow
Craigslist Boats Eugene Oregon
Wsbtv Fish And Game Report
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Adam Bartley Net Worth
Miracle Shoes Ff6
Lovely Nails Prices (2024) – Salon Rates
5A Division 1 Playoff Bracket
3 bis 4 Saison-Schlafsack - hier online kaufen bei Outwell
13 Fun & Best Things to Do in Hurricane, Utah
Ups Authorized Shipping Provider Price Photos
Florida Lottery Powerball Double Play
Dineren en overnachten in Boutique Hotel The Church in Arnhem - Priya Loves Food & Travel
Jesus Calling Oct 6
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 5653

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.