🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeAdding a Legend to Pyplot in Matplotlib in the Simplest Manner

A Legend in a Graph is considered a guide to understanding the graph. When a graph has more than one output, the outputs are usually classified by visually differentiable features such as colors, lines, etc. A legend helps the viewer understand what the graph is implying.
Graphical Visualisation is very important in fields such as Data Analysis and Machine Learning. It helps users classify data, help select features that are important to model, and chart out relations between different features. Graphical visualization helps users to remove redundant features, train models on relationships deduced from the graphs, analyze outliers and remove them.
In Python, the PyPlot module of the Matplotlib library does the task. It allows the user to not only plot the graph but also helps them plot the legends, plotting different graphs such as Line charts, Scatter plots, Bargraph, Histograms, etc., and assigning different colors to the graphs. PyPlot helps in the complete customization of the graph. The plot() method plots the graph based on X Axis and Y Axis inputs, and the show() method visually shows the graph to the user.
Also Read: Python Matplotlib Tutorial
Adding Legend Using Label Parameter of Plot()
This is the simplest method to add a legend to a graph in PyPlot. The plot() method takes a parameter called ‘label,’ which allows the user to define a name for the graph they are plotting. It will be described further, but first, the values to be plotted on X Axis and Y Axis must be defined. They are defined in the code segment below:
import matplotlib.pyplot as plt
X=[i for i in range(-10,10)]
Y1=[(9*x+23) for x in X]
Y2=[((3*x*x)+(8*x)+9) for x in X]
The code above initializes three lists: X, Y1, and Y2. X is a list of integers from -10 to 9. Y1 is a list of inputs from X for a linear equation, while Y2 is a list of outputs based on inputs from X for a quadratic equation. The equations are as shown below:
Linear Equation:
y=9x+23
Quadratic Equation:
y=3x2 + 8x + 9
Now, these inputs can be given to the plot() function of PyPlot, and we can see the plotted graphs. The following piece of code does the same:
plt.plot(X,Y1,'-r',label='Linear Equation')
plt.plot(X,Y2,'-b',label='Quadratic Equation')
plt.legend(loc='upper right')
plt.show()
In the above code, plt is an alias of PyPlot, which was imported in the code snippet above. The plot () method takes input for X Axis and Y Axis as the first two parameters. The ‘-r’ string in the code tells the plot() function to plot a line graph with red color. While the ‘-b’ string tells the plot() function to plot a line graph with blue color. The label parameter of the plot() function lets us name the graphs in our code. We call the graph plotting the linear equation a ‘Linear Equation’ and the graph plotting outputs of a quadratic equation ‘Quadratic Equation.’ The legend will be visible on the graph using the legend() function of the PyPlot. The loc parameter given to the legend() function describes the location of the legend in the graph, which in this case is the upper right corner. plt.show() is used to show the graph visually. The code produces the following output:

Customized Legends in PyPlot
PyPlot allows the users to add customized graph names, which are names that can be imported from iterables like lists and tuples inside the program. This allows a more dynamic approach to the plotting of graphs and is another simpler way of adding legends to a graph. Customized legends are usually used when the data is more complex and detailed. Instead of labeling the graph every time we call the plot() function, the labels are induced by the legend() function through an iterable.
The following code demonstrates customized legends:
plt.plot(X,Y1,'-r')
plt.plot(X,Y2,'-g')
lst=['Linear Equation','Quadratic Equation']
plt.legend(lst,loc='upper right')
This code snippet takes the values of X, Y1, and Y2 from the code snippets above. But here, the plot() function simply takes X-axis values and Y-axis values. The ‘-g’ in Line 10 asks the plot() function to plot the line graph in Green color. The lst variable is a list iterable, which is passed as a reference to the legend() function. The legend() function takes the names from the list iterable and assigns them to plotted graphs. The code produces the following output: