Matplotlib Legend

In this article, we will learn about the concept of legend() method in matplotlib using Python.

A legend can be placed anywhere inside the list or outside the list by a different method and we will see all these methods with examples for each. Adding legend to the plot gives the proper visualization to the graph or diagram, and it also assigns meaning to the various plotting elements.

If we simply add the legend() method in the plot, then it will choose the best location for the placement of legend inside the plot, but if we specifically provide the position of the legend that where it should be placed in the plot(inside or outside). Then it should be placed according to the position provided.

Legend in Matplotlib – Python

Let us understand by some examples:-

Matplotlib legend at best position

import matplotlib.pyplot as plt
import numpy as np

X = np.array([2,4,6,8,10,12,14,16,18,20])
Y = X**2
x = np.arange(10)
fig = plt.figure()

ax = plt.subplot(111)
ax.plot(x, X, label='$y = Line')
ax.plot(x, Y, label='$y2 = Curve')

plt.title('Legend inside')
ax.legend(loc="best")
plt.show()

Output:-

Matplotlib legend at best position

In the above example, we first imported two modules required for the plotting of the graph i.e. matplotib.pyplot and numpy. Then we created a numpy array and stored in a variable X. After that, we created the relation between X and Y, that y is the square of X (Y=X**2). Then we created an object named as fig and ax. And in the legend method, we passed one argument (loc = “best”), which will set the position of legend according to the best location available inside the graph.

Matplotlib legend on bottom

import matplotlib.pyplot as plt
import numpy as np

X = np.array([2,4,6,8,10,12,14,16,18,20])
Y = X**2
x = np.arange(10)
fig = plt.figure()

ax = plt.subplot(111)
ax.plot(x, X, label='$y = Line')
ax.plot(x, Y, label='$y2 = Curve')

plt.title('Legend inside')
ax.legend(loc='center', bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=2)
plt.show()

Output:- 

Matplotlib legend on bottom

In the above example, the explanation is the same as stated in the first example. The only difference is the argument passed inside the legend method (loc=”center”, bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=2), that’s why the position of the legend is in the center bottom.

Matplotlib legend on Top

import matplotlib.pyplot as plt
import numpy as np

X = np.array([2,4,6,8,10,12,14,16,18,20])
Y = X**2
x = np.arange(10)
fig = plt.figure()

ax = plt.subplot(111)
ax.plot(x, X, label='$y = Line')
ax.plot(x, Y, label='$y2 = Curve')

plt.title('Legend inside')
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.00), shadow=True, ncol=2)
plt.show()

Output:-

Matplotlib legend on Top

Matplotlib outside right – legend

import matplotlib.pyplot as plt
import numpy as np

X = np.array([2,4,6,8,10,12,14,16,18,20])
Y = X**2
x = np.arange(10)
fig = plt.figure()

ax = plt.subplot(111)
ax.plot(x, X, label='$y = Line')
ax.plot(x, Y, label='$y2 = Curve')

plt.title('Legend outside')
chartBox = ax.get_position()
ax.set_position([chartBox.x0, chartBox.y0, chartBox.width*0.6, chartBox.height])
ax.legend(loc='upper center', bbox_to_anchor=(1.45, 0.8), shadow=True, ncol=1)
plt.show()

Output:- 

Matplotlib outside right - legend

Leave a Reply

Your email address will not be published. Required fields are marked *