Close Menu
MatlabLegend
    Facebook X (Twitter) Instagram
    MatlabLegend
    • Home
    • Matlab
    • Fashion
    • Gadgets
    • Biography
    • Tech News
    • Tips & Tricks
    MatlabLegend
    Home»Matlab»Add Legend to Graph – Matlab Legend
    Matlab

    Add Legend to Graph – Matlab Legend

    JanisBy JanisJanuary 12, 2025Updated:January 12, 2025No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Add Legend to Graph - Matlab Legend
    Add Legend to Graph - Matlab Legend

    Legend to Graph are essential for labeling data series on a graph. These examples demonstrate how to create a legend and customize it, including adjusting its location, changing the font size, and adding a title. You can also create multi-column legends or limit the legend to a subset of the plotted data.

    Create Simple Legend

    Create a figure that includes both a line chart and a scatter chart. Add a legend to provide descriptions for each chart, specifying the labels directly as inputs to the legend function.

    figure
    x1 = linspace(0,5);
    y1 = sin(x1/2);
    plot(x1,y1)
    
    hold on
    x2 = [0 1 2 3 4 5];
    y2 = [0.2 0.3 0.6 1 0.7 0.6];
    scatter(x2,y2,'filled')
    hold off
    
    legend('sin(x/2)','2016')
    Figure contains an axes object. The axes object contains 2 objects of type line, scatter. These objects represent sin(x/2), 2016.

    Specify Labels Using DisplayName

    You can also define legend labels using the DisplayName property. Set the DisplayName as a name-value pair while calling the plotting functions, and then use the legend command to generate the legend.

    x1 = linspace(0,5);
    y1 = sin(x1/2);
    plot(x1,y1,'DisplayName','sin(x/2)')
    
    hold on
    x2 = [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 update automatically when you add or remove a data series. To specify labels for newly added data, use the DisplayName property. If DisplayName is not set, the legend assigns a default label like 'dataN'.

    Add a scatter chart for the 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')
    drawnow
    hold off
    Figure contains an axes object. The axes object contains 3 objects of type line, scatter. These objects represent sin(x/2), 2016, 2017.

    Customize Legend Appearance

    The legend function generates a Legend object, which comes with customizable properties like Location, Orientation, FontSize, and Title. For a comprehensive list of options, refer to Legend Properties.

    You can customize these properties in two ways:

    • Using Name-Value Pairs:
      Specify properties directly in the legend command using name-value pairs. When using this method, the labels must often be provided in a cell array.
      Example:
      legend({'label1', 'label2'}, 'FontSize', 14)
    • Using the Legend Object:
      Retrieve the Legend object as an output argument using the legend function (e.g., lgd = legend). Then, set properties using dot notation.
      Example:

    Legend Location and Orientation

    Set the legend’s location and orientation using the Location and Orientation properties as name-value pairs. Choose a location from the eight cardinal or intercardinal directions, such as ‘northwest’. Adjust the orientation to ‘vertical’ (default) or ‘horizontal’, as shown here. Define the labels using a cell array.

    x1 = linspace(0,5);
    y1 = sin(x1/2);
    plot(x1,y1)
    
    hold on
    x2 = [0 1 2 3 4 5];
    y2 = [0.2 0.3 0.6 1 0.7 0.6];
    scatter(x2,y2,'filled')
    hold off
    
    legend({'sin(x/2)','2016'},'Location','northwest','Orientation','horizontal')
    Figure contains an axes object. The axes object contains 2 objects of type line, scatter. These objects represent sin(x/2), 2016.

    Legend Font Size and Title

    Set the legend’s font size and title by adjusting the FontSize and Title properties. Assign the Legend object to a variable (e.g., lgd), and modify its properties using dot notation.

    x1 = linspace(0,5);
    y1 = sin(x1/2);
    plot(x1,y1,'DisplayName','sin(x/2)')
    
    hold on
    x2 = [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 off
    
    lgd = legend;
    lgd.FontSize = 14;
    lgd.Title.String = '2016 Data';
    Figure contains an axes object. The axes object contains 2 objects of type line, scatter. These objects represent sin(x/2), 2016.

    Legend with Multiple Columns

    Create a chart featuring six line plots, and configure 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 on
    plot(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 off
    
    lgd = legend;
    lgd.NumColumns = 2;
    Figure contains an axes object. The axes object contains 6 objects of type line. These objects represent sin(x), sin(0.9x), sin(0.8x), sin(0.7x), sin(0.6x), sin(0.5x).

    Include Subset of Charts in Legend

    Combine two bar charts and a scatter chart. To create a legend that includes only the bar charts, pass the Bar objects b1 and b2 as a vector in the first input argument of the legend function.

    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 off
    
    legend([b1 b2],'Bar Chart 1','Bar Chart 2')
    Figure contains an axes object. The axes object contains 3 objects of type bar, scatter. These objects represent Bar Chart 1, Bar Chart 2.
    Janis
    • Website

    Janis is the creator of Matlab Legend, an engineer and tech enthusiast passionate about simplifying MATLAB, AI, and tech concepts. Through practical guides and insights, they aim to empower learners and professionals worldwide.

    Related Posts

    Legend Behavior in Data Visualization: Automatic Labeling and Updates

    February 5, 2025

    Greek Letters and Special Characters in Chart Text

    January 31, 2025

    Make the Graph Title Smaller

    January 31, 2025
    Leave A Reply Cancel Reply

    Search
    Recent Posts

    How to Book a Bus from Kelantan to Melaka Online

    April 2, 2026

    Kineret Karen Ben Yishay Moore: Exploring the Meaning, Heritage, and Identity Behind the Name

    February 28, 2026

    Kevin Corke Biography: Wife, Spouse, and Family Insights

    February 19, 2026

    Elena Moussa Height: Discover How Tall Greg Gutfeld’s Wife Really Is and Why It Matters

    February 18, 2026

    Kurt Perez and The Blacklist: The Real Story Explained

    February 17, 2026

    Jaguar Wright’s Net Worth in 2026: Inside the Neo-Soul Singer’s $2.5 Million Success Story

    February 17, 2026
    About Us

    MatlabLegend is your go-to hub for mastering MATLAB with clarity and confidence. Explore expert insights, step-by-step tutorials, and practical guides designed for beginners and professionals alike.

    Whether you're starting out or advancing research and engineering projects, MatlabLegend helps you learn faster and apply MATLAB skills effectively every day. #MatlabLegend

    Popular Posts

    How to Book a Bus from Kelantan to Melaka Online

    April 2, 2026

    Kineret Karen Ben Yishay Moore: Exploring the Meaning, Heritage, and Identity Behind the Name

    February 28, 2026
    Contact Us

    Phone: Whatsapp

    Mail: tech4links@gmail.com

    เว็บหวยออนไลน์ | UFABET | kết quả bóng đá

    Copyright © 2026 | All Rights Reserved | MatlabLegend
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    • Write for Us
    • Sitemap

    Type above and press Enter to search. Press Esc to cancel.

    WhatsApp us