Change the background color of a Matplotlib legend

Python

Matplotlib

Legend

Change color

Luc B.

Python

Matplotlib

Legend

Matplotlib allows changing the background color of a legend. Users can use this to make legends stand out in a busy plot or to just add style.

Code Example

Use the facecolor argument to plt.legend() to specify the legend background color.

import matplotlib.pyplot as plt

plt.plot([0, 1], [0, 1.0], label='Label 1')
plt.plot([0, 1], [0, 1.1], label='Label 2')
plt.plot([0, 1], [0, 1.2], label='Label 3')

# Set the legend background color here
plt.legend(facecolor="yellow")

plt.show()

png

facecolor can be any valid matplotlib color argument.