EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • 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 Matplotlib Legend
 

Matplotlib Legend

Priya Pedamkar
Article byPriya Pedamkar

Updated March 1, 2023

Matplotlib Legend

 

 

Introduction to Matplotlib Legend 

In this article, we will learn about a method called ‘Legend’ in Python and matplotlib. We will be creating labels using the ‘Legend’ method. Before we start creating labels for our plots, let us quickly understand what is the significance of creating plots and why we need labels for them.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

  1. Plots are created for visualizing the data and get the inference out of it in a single view
  2. Plots are very useful in understanding any correlation between our variables, which we can utilize further in statistical concepts like linear regression&multiple regression
  3. As multiple functions can be drawn in a single plot; ‘labels’ are the way to provide information about different functions in the plot and labels are created using ‘Legends’ in Python.

Examples to Implement Matplotlib Legend

Let us now understand how to create plots and give them labels, in python using matplotlib:

Example #1

Importing Python libraries:

import numpy as np [importing 'numpy']
import matplotlib.pyplot as plt  [importing ‘matplotlib’]

Next, let us define our functions for the plot. For this example, we will use sine and cosine functions

a = np.arange(0, 10, 0.1)
b = np.sin(a) [creating a sine wave]
c = np.arange(0, 10, 0.1)
d = np.cos(c) [creating a cos function]
z = plt.subplot(111) [creating subplots to include both functions in single plot]
z.plot(a,b, label = "sine function") [drawing the plot and defining the label for sine wave]
z.plot(c,d, label = "cos function") [drawing the plot and defining the label for sine wave]
plt.title ('Learning legends') [giving a name to our plot]
plt.legend (loc=1)   [loc is used to control the direction of legend]
plt.show() [calling show() to display our pot]

Code:

import numpy as np
import matplotlib.pyplot as plt
a = np.arange(0, 10, 0.1)
b = np.sin(a)
c = np.arange(0, 10, 0.1)
d = np.cos(c)
z = plt.subplot(111)
z.plot(a,b, label = "sine function")
z.plot(c,d, label = "cos function")
plt.title ('Learning legends')
plt.legend (loc=1)
plt.show()

Output:

sine and cosine functions

Note: The label box on the top right side. This direction is obtained because we have passed the argument ‘loc’ = 1 in our legend method. We can set ‘loc’ to different integer values to change the direction of the label box within the plot.

Let us change the value of loc to 3 and see how our output changes.

Our code will essentially be the same as above, with only change that loc will now be 3:

plt.legend (loc= 3)

This is how our output will look like in python:

learning legends

Note that our label box is now in the bottom left. So, we can get the direction as per our requirement, by changing the value of ‘loc’.

Example #2

Next, we will understand how to set our label box outside the plot. We will create a new plot of 2 straight lines to understand this:

Importing Python libraries:

Next we will understand how to set our label box outside the plot. We will create a new plot of 2 straight lines to understand this.

import matplotlib.pyplot as plt[importing 'matplotlib']
import numpy as np[importing 'numpy']
a = [12, 14, 16, 18, 20, 22, 24, 26, 28, 30][Defining our variables to create straight lines]
b = [4.1, 8.1, 12.1, 16.1, 20.1, 24.1, 28.1, 32.1, 36.1, 40.1]
c = [5.2, 10.2, 15.2, 20.2, 25.2, 30.2, 35.2, 40.2, 45.2, 50.2]
fig = plt.figure()
z = plt.subplot(111)[creating subplots to include both functions in single plot]
z.plot(a, c, label='Line 1')[drawing the plot and defining the label for first line]
z.plot(b, c, label='Line 2')[drawing the plot and defining the label for second line]
plt.title('Legend outside the plot')[giving a name to our plot]

Note that In next line of code, we have added a couple of new arguments. Passing these pre-defined codes as an argument will help us to create the label box outside our plot

z.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol = 2)[ncol = 2, will draw a label box with 2 columns]
plt.show() [Finally, displaying our plot]

Code:

import matplotlib.pyplot as plt
import numpy as np
a = [12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
b = [4.1, 8.1, 12.1, 16.1, 20.1, 24.1, 28.1, 32.1, 36.1, 40.1]
c = [5.2, 10.2, 15.2, 20.2, 25.2, 30.2, 35.2, 40.2, 45.2, 50.2]
fig = plt.figure()
z = plt.subplot(111)
z.plot(a, c, label='Line 1')
z.plot(b, c, label='Line 2')
plt.title('Legend outside the plot')
z.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol = 2)
plt.show()

Output:

legend outside the plot

Explanation: As we can see in our output, the label box is now outside the plot and with 2 columns.

Conclusion

Plots become very handy when we are trying to understand the data intuitively. They help us in understanding any relation between the variables and also to have a high-level understanding of data without actually viewing it. The ‘Legend’ method in matplotlib is used to create labels for the plots, which help us in differentiating the functions drawn in the plot.

Recommended Articles

This is a guide to Matplotlib Legend. Here we discuss an introduction to Matplotlib Legend, along with examples in detail explanation for better understanding. You can also go through our other related articles to learn more –

  1. Polar Plot in Matlab
  2. Data Visualization Tools
  3. Matlab color()
  4. Quiver Matlab 

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

© 2025 - 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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW