Add a title to a Matplotlib legend

Python

Matplotlib

Legend

Add title

Luc B.

Python

Matplotlib

Legend

Legend titles are useful for adding context to a visualization, further increasing clarity.

Code Example

Use the title argument to plt.legend() to specify the legend title.

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 title here
plt.legend(title="Legend Title")

plt.show()

png