Matplotlib.axes.Axes.legend() in Python

Legend() is the part of the matplotlib library that creates the box-like structure on the plot, which describes the information about the elements present.

Matplotlib.axes.Axes.legend() in Python

Syntax

  • *args: This feature enables you to provide different inputs to the function.
  • **kwargs: This stands for keyword arguments and allows passing additional parameters to customize legend appearance and position. Commonly used arguments include loc, font size, and title.

Not Specifying any Labels And Calling the legend() Function

If we use the legend() function without specifying the labels of the elements of the graph, it will not create any box-like structure on the graph.

Example

Here is a clear example of calling legend() without specifying the labels:

Program

Output:

Matplotlib.axes.Axes.legend() in Python

Explanation

If we call the legend() function without specifying the labels, it will create the plot without any legend.

Elements to be Displayed in the Legend are Automatically Detected

Example

Let us consider an example program:

Program

Output:

Matplotlib.axes.Axes.legend() in Python

Explanation

We have defined the x, y1 and y2 points in this example to plot a graph. Then, we used the legend() method to add labels about the axes present on the graph. Instead of adding the labels with a separate function, we used the plot function. The legend() function takes the labels defined while plotting as input.

Passing Additional Parameters

Example

Here is an example program:

Program

Output:

Matplotlib.axes.Axes.legend() in Python

Explanation

In this example, we have defined the x, y1 and y2 points to plot the graph then we used the legend() method to add labels about the axes present on the graph for that, we used the legend() function with the loc, fontsize, and title parameters to customize the appearance and position of the legend here we are not adding the labels directly in the function instead legend() function takes the labels defined while plotting.

Setting Labels Using set_label

Setting labels using set_label will add the label to the axes present on the graph, and if we use the legend() function without passing the parameter, it will detect the labels set by the set_label function and create the legend with the labels.

Example

Let us consider an example program.

Program

Output:

Matplotlib.axes.Axes.legend() in Python

Explanation

In this example, we imported the required library and then defined the data points to plot with the help of the plot() function. We plotted each data, then used the set_lebel() method for each line, and the legend function detected the labels and created the legend. Finally, we displayed the plot using the show() method.

legend() With Scatter Plot

Example

Here is an example program:

Program

Output:

Matplotlib.axes.Axes.legend() in Python

Explanation

In this example, we have two sets of data points named y1 and y2, respectively, that are plotted against the same set of x-values. The scatter() function creates scatter plots for each dataset, while the label parameter specifies each label. Finally, the legend() function is called without any arguments to automatically include the labels specified in the scatter function. The resulting legend will display the labels and their corresponding marker styles and colours.

Customizing the Position of the Legend

To change the legend's position in a plot using Matplotlib, you can use the loc parameter of the legend function. The loc parameter accepts different string values representing various positions for the legend. Some examples of common values for the loc parameter are:

  • 'upper right'
  • 'upper left'
  • 'lower left'
  • 'lower right'
  • 'right'
  • 'center left'
  • 'center right'
  • 'lower center'
  • 'upper center'
  • 'center'

Example

Here is an example program to set the legend position to the lower left:

Program

Output:

Matplotlib.axes.Axes.legend() in Python

Explanation

Here, we have specified the position of the legend as upper right using the loc parameter in the legend function.

Placing the Legend Outside the Plot in Matplotlib

We can use the bbox_to_anchor parameter with the loc parameter to place the legend outside the plot.

Example

Let us see an example program to place the legend outside the plot.

Program

Output:

Matplotlib.axes.Axes.legend() in Python

Explanation

In the provided example, the parameter bbox_to_anchor has been set to (1,0.5), indicating that the legend has been placed on the right side of the plot. You can modify the values of bbox_to_anchor to change the legend's position.

Conclusion

In conclusion, the `legend()` function in Matplotlib is a powerful tool for enhancing the interpretability of plots by providing labels for various elements within a graph. It is extensively employed to differentiate between disparate data series and is particularly advantageous when multiple lines, markers, or other plot elements are present. The function provides a straightforward means of incorporating a legend, with default positioning that minimally disrupts the plot. Additionally, users can effortlessly customize the legend's location, font size, frame appearance, and background colour to meet specific visualization requirements. Regardless of whether the legend is positioned within or outside the plot, the `legend()` function offers flexibility and versatility, thereby contributing to the overall clarity and communicative efficacy of Matplotlib visualizations.