Adding and customising legends in Matplotlib
Learn to add legends to Matplotlib plots and customise them in Python.

Topics Covered
- What is the use of legend in Matplotlib?
- How to add a legend in Matplotlib?
- How to change the position of a legend in Matplotlib?
- How to change the font size of a legend in Matplotlib?
- How to add color to a legend in Matplotlib?
- How to add a legend title in Matplotlib?
What is the use of legend in Matplotlib?
A legend helps you understand the different datasets plotted in your graph. They help provide meaning to various elements of the visualization.
How to add a legend in Matplotlib?
In Matplotlib, you can use the legend()
function to create legends. Let's build a simple plot showing both sin
and cosine
functions.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), '-b', label='Sine')
ax.plot(x, np.cos(x), '--r', label='Cosine')
ax.axis('equal')
leg = ax.legend()
plt.show()

How to change the position of a legend in Matplotlib?
You can use the loc()
parameter to change the position of a legend in a matplotlib plot.
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2, 2)
x = np.linspace(0, 10, 1000)
fig, axs = plt.subplots(2,2)
legend_loc = ['upper left', 'upper right', 'bottom left', 'bottom right']
axs[0,0].plot(x, np.sin(x), '-b', label='Sine')
axs[0,0].plot(x, np.cos(x), '--r', label='Cosine')
axs[0,0].axis('equal')
axs[0,0].legend(loc='upper left')
axs[0,1].plot(x, np.sin(x), '-b', label='Sine')
axs[0,1].plot(x, np.cos(x), '--r', label='Cosine')
axs[0,1].axis('equal')
axs[0,1].legend(loc='upper right')
axs[1,0].plot(x, np.sin(x), '-b', label='Sine')
axs[1,0].plot(x, np.cos(x), '--r', label='Cosine')
axs[1,0].axis('equal')
axs[1,0].legend(loc='lower left')
axs[1,1].plot(x, np.sin(x), '-b', label='Sine')
axs[1,1].plot(x, np.cos(x), '--r', label='Cosine')
axs[1,1].axis('equal')
axs[1,1].legend(loc='lower right')
plt.show()

How to change the font size of a legend in Matplotlib?
You can use the fontsize
parameter to change the size of legend in Matplotlib. The accepted values are xx-small, x-small, small, medium, large, x-large and xx-large
.
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2)
x = np.linspace(0, 10, 1000)
fig, axs = plt.subplots(2)
legend_loc = ['upper left', 'upper right', 'bottom left', 'bottom right']
axs[0].plot(x, np.sin(x), '-b', label='Sine')
axs[0].plot(x, np.cos(x), '--r', label='Cosine')
axs[0].axis('equal')
axs[0].legend(loc='upper left', fontsize='x-small')
axs[1].plot(x, np.sin(x), '-b', label='Sine')
axs[1].plot(x, np.cos(x), '--r', label='Cosine')
axs[1].axis('equal')
axs[1].legend(loc='upper right', fontsize='x-large')
plt.show()

How to add color to a legend in Matplotlib?
With certain plots, it might be useful to add colors to your legends. There are 3 parameters that you can use to specify colors to a legend.
labelcolor - used to change the color of the text.
facecolor - used to change background color of the legend.
edgecolor - used to change the edge color of the legend.
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2)
x = np.linspace(0, 10, 1000)
fig, axs = plt.subplots(2)
legend_loc = ['upper left', 'upper right', 'bottom left', 'bottom right']
axs[0].plot(x, np.sin(x), '-b', label='Sine')
axs[0].plot(x, np.cos(x), '--r', label='Cosine')
axs[0].axis('equal')
axs[0].legend(loc='upper left', fontsize='x-small', labelcolor='white', facecolor='black',
edgecolor='red')
axs[1].plot(x, np.sin(x), '-b', label='Sine')
axs[1].plot(x, np.cos(x), '--r', label='Cosine')
axs[1].axis('equal')
axs[1].legend(loc='upper right', fontsize='x-large', labelcolor='brown', facecolor='white',
edgecolor='black')
plt.show()

How to add a legend title in Matplotlib?
To add a legend title in Matplotlib, use the title
parameter. You can increase or decrease its size using the title_fontsize
parameter.
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2)
x = np.linspace(0, 10, 1000)
fig, axs = plt.subplots(2)
legend_loc = ['upper left', 'upper right', 'bottom left', 'bottom right']
axs[0].plot(x, np.sin(x), '-b', label='Sine')
axs[0].plot(x, np.cos(x), '--r', label='Cosine')
axs[0].axis('equal')
axs[0].legend(loc='upper left', title = 'Functions', title_fontsize ='x-small')
axs[1].plot(x, np.sin(x), '-b', label='Sine')
axs[1].plot(x, np.cos(x), '--r', label='Cosine')
axs[1].axis('equal')
axs[1].legend(loc='upper right', title = 'Functions', title_fontsize ='x-large')
plt.show()

Pylenin has a dedicated Youtube playlist for Matplotlib Tutorial. Check out our entire Matplotlib playlist here.