Open In App

Matplotlib.pyplot.figlegend() function in Python

Last Updated : 18 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a Python library used for creating, animations, and editing graphs, plots, and figures using Pyplot. Matplotlib.pyplot has many functions defined in it to use, as per the preference and requirement of the user demands.

matplotlib.pyplot.figlegend() function

 This is used to place a legend on the figure. A legend in Matplotlib is similar to a nameplate that defines the cures in the figure.

Syntax: matplotlib.pyplot.figlegend(*args, **kwargs)
Parameters: some important parameters are:

  • handles: list of all figure lines(or in technical tern Artist) A list of Artists (lines, patches) to be added to the legend is given to be the different handles. Specifying handles is optional in figlend().
  • labels: list of actual name of the legends A list of labels to show what is the actual value of the Artist are called labels. Specifying labels is optional in figlend() and if labels are not specified then the figlend() function will name them Legend 1, Legend 2 and so on.
  • loc : Location of the legend(default: ‘best’).

Returns: returns the Legend to be put on figure.

Example 1: Created a data set x with values x = [0, 0.1, 0.2,….,5] and y = sin(x) and then plotted the figure with dataset x at x-axis and y at y-axis with the label = ” Sin” and plotted with the figure with default figlegend() with uses the before specified label as a legend.

Python3




# Importing the necessary modules
import numpy as np
import matplotlib.pyplot as plt
  
# Creating a dataset
x = np.arange(0, 5, 0.1)
y = np.sin(x)
  
# Plotting the data
plt.plot(x, y, label = "Sin")
  
# Legend
plt.figlegend()


Output: 

figlegend()

Example 2: Using the same approach as above but showing the use of figlegend() handle and label but using the default position.

Python3




# Importing the necessary modules
import numpy as np
import matplotlib.pyplot as plt
  
# Creating a dataset
x = np.arange(0, 5, 0.1)
y = np.cos(x)
  
# Plotting the data
line1 = plt.plot(x, y)
  
# Legend
plt.figlegend((line1),('Cos'))


Output: 

figlegend plot-2

Example 3: Showing the use of figlegend(handles, labels, location) function by plotting two figures on one plot of tan and cos function.

Python3




# Importing the necessary modules
import numpy as np
import matplotlib.pyplot as plt
  
# Creating a dataset
x = np.arange(0, 5, 0.1)
y = np.tan(x)
  
x1 = np.arange(0, 5, 0.1)
y1 = np.cos(x)
  
# Plotting the data
line1 = plt.plot(x, y)
line2 = plt.plot(x1, y1)
  
# Legend
plt.figlegend(
handles = (line1,line2),
          labels = ("Tan","Cos"),
          loc='upper right')


Output: 

figlegend plot-3



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads