python logo

Matplotlib legend


Python hosting: Host, run, and code Python in the cloud!

Matplotlib is a versatile Python library that provides native support for creating legends in various visualizations. Understanding how to position legends, whether inside or outside a chart, can enhance data interpretation. This article offers a comprehensive guide on leveraging the legend() function in matplotlib for enhancing your data visualizations.

Related Course:

Positioning the Matplotlib Legend Inside the Chart

Utilizing matplotlib’s legend() function, one can seamlessly position the legend within the chart. Here’s a practical example:

import matplotlib.pyplot as plt
import numpy as np

y = [2,4,6,8,10,12,14,16,18,20]
y2 = [10,11,12,13,14,15,16,17,18,19]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
ax.plot(x, y2, label='$y2 = other numbers')
plt.title('Legend Inside the Plot')
ax.legend()
plt.show()

Matplotlib legend positioned inside the plot

Positioning the Matplotlib Legend at the Bottom

To move the legend to the bottom of your chart, you can adjust the legend() parameters as shown:

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=2)

Note the introduction of the ncol=2 parameter, which sets the number of columns in the legend. Additionally, a shadow effect has been added for aesthetic purposes.

Complete code example:

import matplotlib.pyplot as plt
import numpy as np

y = [2,4,6,8,10,12,14,16,18,20]
y2 = [10,11,12,13,14,15,16,17,18,19]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
ax.plot(x, y2, label='$y2 = other numbers')
plt.title('Legend at the Bottom')
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=2)
plt.show()

Legend positioned at the bottom of the plot

Positioning the Matplotlib Legend at the Top

To shift the legend to the top of your visualization, modify the bbox_to_anchor values:

ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.00), shadow=True, ncol=2)

Complete code for this setup:

import matplotlib.pyplot as plt
import numpy as np

y = [2,4,6,8,10,12,14,16,18,20]
y2 = [10,11,12,13,14,15,16,17,18,19]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
ax.plot(x, y2, label='$y2 = other numbers')
plt.title('Legend at the Top')
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.00), shadow=True, ncol=2)
plt.show()

Legend positioned at the top of the plot

Placing the Legend Outside to the Right

To locate the legend externally on the right, we need to resize the plot and set the legend’s position relative to it:

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)

Here’s a complete implementation:

import matplotlib.pyplot as plt
import numpy as np

y = [2,4,6,8,10,12,14,16,18,20]
y2 = [10,11,12,13,14,15,16,17,18,19]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
ax.plot(x, y2, label='$y2 = other numbers')
plt.title('Legend Positioned Outside to the Right')
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()

Matplotlib legend positioned externally to the right

To further enhance your matplotlib capabilities, download more examples. If you wish to explore other plotting techniques, you can navigate backward or forward.






Leave a Reply: