Matplotlib.pyplot.figlegend() function in Python (original) (raw)

Last Updated : 18 Aug, 2020

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:

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