51

Is there a way to change the font color of the legend in a matplotlib plot?

Specially in occasions where the background of the plot is dark, the default black text in the legend is hard or impossible to read.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
firefly
  • 953
  • 2
  • 11
  • 18
  • You can also pass in what ever font properties you want through the kwarg `prop` http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.legend – tacaswell Sep 20 '13 at 13:30

5 Answers5

46

call Legend.get_texts() will get a list of Text object in the legend object:

import pylab as pl
pl.plot(randn(100), label="randn")
l = legend()
for text in l.get_texts():
    text.set_color("red")
HYRY
  • 94,853
  • 25
  • 187
  • 187
37

As of matplotlib version 3.3.0, you can now directly use the keyword argument labelcolor in matplotlib.pyplot.legend().


Example using the same color as the corresponding artist by setting labelcolor='linecolor':

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(4, 3))
plt.plot(np.arange(10), np.random.rand(10) * 0, '-', label='spam')
plt.plot(np.arange(10), np.random.rand(10) * 1, ':', label='ham')
plt.plot(np.arange(10), np.random.rand(10) * 2, 'o', label='eggs')
plt.legend(labelcolor='linecolor')

matplotlib legend text color: 'linecolor'


Example changing all text to white by setting labelcolor='w', e.g. for dark backgrounds:

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(4, 3))
plt.plot(np.arange(10), np.random.rand(10) * 0, '-', label='spam')
plt.plot(np.arange(10), np.random.rand(10) * 1, ':', label='ham')
plt.plot(np.arange(10), np.random.rand(10) * 2, 'o', label='eggs')
plt.legend(facecolor='k', labelcolor='w')

matplotlib legend text color: all text white for dark backgrounds

Zaus
  • 1,089
  • 15
  • 25
20

Because plt.setp broadcasts over iterables, you can also modify the text color in one line:

# Show some cool graphs
legend = plt.legend()
plt.setp(legend.get_texts(), color='w')

The last line will apply the colour to all elements in the collection of texts.

Till Hoffmann
  • 9,479
  • 6
  • 46
  • 64
17

You can also do it with setp():

import pylab as plt

leg = plt.legend(framealpha = 0, loc = 'best')
for text in leg.get_texts():
    plt.setp(text, color = 'w')

this method also allows you to set the fontsize and any number of other font properties in one line (listed here: http://matplotlib.org/users/text_props.html)

full example:

import pylab as plt

x = range(100)
y1 = range(100,200)
y2 = range(50,150)

fig = plt.figure(facecolor = 'k')
ax = fig.add_subplot(111, axisbg = 'k')
ax.tick_params(color='w', labelcolor='w')
for spine in ax.spines.values():
    spine.set_edgecolor('w')
ax.plot(x, y1, c = 'w', label = 'y1')
ax.plot(x, y2, c = 'g', label = 'y2')

leg = plt.legend(framealpha = 0, loc = 'best')
for text in leg.get_texts():
    plt.setp(text, color = 'w')

plt.show()
wordsforthewise
  • 13,746
  • 5
  • 87
  • 117
3

Anyone looking to change the color of the TITLE of the legend; seems to only be available through the side door:

leg._legend_title_box._text.set_color('#FFFFFF')
tdy
  • 36,675
  • 19
  • 86
  • 83
Ryan Skene
  • 864
  • 1
  • 12
  • 29