EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Plot Legend

Matlab Plot Legend

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Matlab Plot Legend

Introduction to MATLAB Plot legend

MATLAB provides plenty of functionalities useful in various computational problems. As we learned in previous articles, we can create vector plots in MATLAB using the ‘plot’ function. This article will teach us how to put Legends into the plots created in MATLAB. First, let us understand why we need to have legends for our plots. We create plots to visualize our data, and as we learned already, a single plot can have more than 1 vector or function. In such cases, we must provide more info about the functions drawn in the plot Legends are the way to provide information about different functions contained in a single plot In this topic, we are going to learn about Matlab Plot Legend.

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax

The syntax for creating Legends in MATLAB:

legend
legend(L1, L2, ...., L N) , where L1, L2 and so on represents the respective labels.

Explanation: This function will create a legend for each data series used in the plot, with descriptive labels. The function ‘legend’ will create labels like ‘data1’, ‘data2’, and so on.

Table for direction codes

Direction Code
Top of the axes north
Bottom of the axes south
Right of the axes east
Left of the axes west
Top right the axes northeast
Top left of the axes northwest
Bottom right southeast
Bottom left southwest

Examples to Implement Matlab Plot Legend

Let us understand the function with an example:

Example #1

First, we will define ‘A’ as a vector containing values between 2pi (π) and 3π. We will define an increment of π/50 between these values.

Next, we will define B as the cos function of values of A and C as sine function of values of A

A = 2*pi : pi/50 : 3*pi

B = cos (A)

C = sin (A)

Now to understand how ‘Legend’ works, we will first plot our input functions and then use the function ‘legend’.

Our inputs A, B& C are first passed as arguments to the function ‘plot’.

And then we simply write ‘legend’ in our code to get the labels.

This is how our input and output will look like in MATLAB console:

Code:

A = 2*pi : pi/50 : 3*pi
B = cos (A)
C = sin (A)
figure
plot(A,B,A,C)
legend

Output:

Matlab Plot Legend1

Explanation: As we can see in the above output, we have plotted 2 vectors and our legend function created corresponding labels. Since nothing was passed as an argument to legend function, MATLAB created labels as ‘data1’ and ‘data2’.

Example #2

Now, what if instead of ‘data1’ and ‘data2’, we want to have the name of the function as the label.

Let us learn how to do it.

Our initial code will be the same as in the above example

A = pi : pi/100 : 3*pi

b = cos (A)

c = sin (A)

In addition to the above code, we will add the below-mentioned line:

legend({'sin(A)', 'cos(A)'})

As we can see, we have passed the name of the functions as an argument to our legend function. Here, we can name our functions as per our needs.

This is how our input and output will look like in MATLAB console:

Code:

A = pi : pi/100 : 3*pi
b = cos (A)
c = sin (A)
figure
plot (A, b, A, c)
legend({'sin(A)','cos(A)'})

Output:

function as the label

Explanation: Notice on the top right side of the plot, we have got the names of our functions.

Example #3

Next, what if we don’t want our labels to be on the top right but in some other place on the plot.

Let us learn how to achieve that.

Legend function in MATLAB allows us to put our label in place of our choice. All we need to do is pass the pre-defined code for the direction, as an argument.

Our initial code will not change:

A = pi : pi/100 : 3*pi

b = cos (A)

c = sin (A)

In addition to the above code, we will add the below-mentioned line:

legend ({'sin(A)', 'cos(A)'}, 'Location','northwest')

Notice that in addition to the names of the functions, we have added 2 more arguments:

‘Location’ & ‘northwest’. Here ‘Location’ is a keyword for setting the direction of our labels and ‘northwest’ is pre-defined code for direction (Please refer to Table the end of the article to get pre-defined direction codes).

This is how our input and output will look like in MATLAB console:

Code:

A = pi : pi/100 : 3*pi
b = cos (A)
c = sin (A)
figure
plot (A, b, A, c)
legend({'sin(A)','cos(A)'},'Location','northwest')

Output:

pre-defined direction

Explanation: As we can clearly see in our output, the label is now on the top left (north west direction).

Example #4

Next, we will learn how to give a name to our label box. i.e we want to convey that sin(A) and cos(A) are our ‘FUNCTIONS’.

Here also our initial code will be the same as above. In addition to that, here we will add the following code:

title(legend,'FUNCTIONS’)

This is how our input and output will look like in MATLAB console:

Code:

A = 2*pi : pi/500 : 3*pi
b = cos (A)
c = sin (A)
figure
plot (A, b, A, c)
legend({'sin(A)','cos(A)'},'Location','northwest')
title(lgd, 'Functions')

Output:

Matlab Plot Legend4

Explanation: As we can notice in the output, our label box is now named.

Conclusion

We learned how to create labels in MATLAB plots and achieve desired styles. We also learned to set the ‘direction’ and ‘Name’ of the label box as per our needs. Labels become very important when we plot multiple functions in the same graph.

Recommended Articles

This is a guide to Matlab Plot Legend. Here we have discussed an introduction to Matlab Plot Legend with appropriate syntax and respective programming examples. You can also go through our other related articles to learn more –

  1. Matlab Stem()
  2. Matlab Legend
  3. MATLAB Interpolation
  4. Polar Plot in Matlab
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW