Adding a Legend to the Plot
Discover how to add legends to Matplotlib plots to distinguish multiple line elements. Learn two methods for setting legend labels and explore customization options such as location, font size, columns, and background to improve chart readability.
We'll cover the following...
What is a legend?
A legend provides a description for an element in the figure. A legend helps the user understand a figure when the figure contains many elements. For example, if there are two curves on a graph, then the legend can help the user distinguish between the curves.
There is more than one way to add a legend to a figure. In this lesson, we will learn two methods. Both use the plot() function. The difference, however, is in how the two methods set their labels.
Adding a legend without labels
In this method, we don’t pass the labels to the legend(), because the labels are passed in plot(). When legend() is called, the Axes would detect the labels ...