How to Put a Legend on a MATLAB Plot

Written by michael peter
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Put a Legend on a MATLAB Plot
A legend labels the lines represented on the plot. (Hemera Technologies/ Images)

MATLAB is a technical computing program used in the engineering and scientific community for data processing and analysis. MATLAB allows the user to manipulate large data sets typically represented in arrays and matrices. You need to visualise data after they are in the correct form. Multiple data sets plotted on the same graph require a legend to differentiate between each line.

Skill level:
Moderately Easy


  1. 1

    Clear all active variables in the workspace by entering "clear all" in the "Command" window.

  2. 2

    Plot the data. Enter the appropriate plotting function into the command window. To create sample data input "x=(1:1:10);" and "y=rand(1,10);". Next enter "plot(x,y);". into the command window. Do not press "Enter."

  3. 3

    Enter on the same line of code "legend('dataname')" where dataname is the name shown on the legend included on the plot. Press "Enter." The legend appears on the top right of the plot.

  4. 4

    Click "Show Plot Tools and Dock Figure." Once "Plot Tools" opens, click on the legend that displays a location selector on the bottom left. Select an appropriate location for the legend.

Tips and warnings

  • If the data are already plotted, click "Insert-Legend" in the "Figure" menu.
  • If multiple data sets are plotted on the same graph, enter legend('dataset1', 'dataset2') where dataset1 and dataset2 are the names of the data.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.