Matplotlib.pyplot.figlegend() function in Python
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:
- 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:
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:
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:
Similar Reads
Matplotlib.pyplot.figimage() function in Python Matplotlib is a widely used library in Python for plotting various graphs, as it provides very efficient ways and easy to understand methods for complex plots also. pyplot is a collection of command style functions that make matplotlib work like MATLAB. figimage() function matplotlib.pyplot.figimage
2 min read
Matplotlib.pyplot.get_figlabels() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.setp() function in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.legend() in Python A legend is an area describing the elements of the graph. In the Matplotlib library, thereâs a function called legend() which is used to place a legend on the axes. In this article, we will learn about the Matplotlib Legends.Python Matplotlib.pyplot.legend() SyntaxSyntax: matplotlib.pyplot.legend(["
6 min read
matplotlib.pyplot.figure() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.get_fignums() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read