3

In the example below, how can I change the background color of "Title" to be red (with alpha=.5), not the font color of "Title"?

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,], [1,2,3], label='Series')
legend = ax.legend(title='Title')

# Access to the legend title box adapted from
# https://stackoverflow.com/a/63570572
legend._legend_title_box._text.set_color('red')

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
jII
  • 1,134
  • 2
  • 17
  • 29

4 Answers4

4

You can change the color of the bounding box of the title text:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3, ], [1, 2, 3], label='Series')
legend = ax.legend(title='Title')

legend._legend_title_box._text.set_bbox(dict(facecolor='red', alpha=0.3, lw=0))

plt.show()

change background of title box of legend

PS: For the red background to span the entire width of the legend frame, you could pad the title with whitespace, as proposed by Trenton Mckinney. E.g.

legend = ax.legend(title=f'{"Title": ^15}', frameon=False)

fill the background to width

JohanC
  • 71,591
  • 8
  • 33
  • 66
1

Before writing this, I should specify that this is potentially fragile. But fundamentally things are artists in matplotlib, and with enough determination one can alter the various artists there.

This is how to make just the background of Title be red, with an alpha of 0.5. More generally, this is how one can obtain this patch, which allows whatever other adjustments you might want.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,], [1,2,3], label='Series')
legend = ax.legend(title='Title')
t = legend._legend_title_box._text
t.set_backgroundcolor('r')
t._bbox_patch.set_alpha(0.5)  # <--- this is the patch

A matplotlib image with a colored title background

davidlowryduda
  • 2,404
  • 1
  • 25
  • 29
0

I don't think there is access to this

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.plot([1,2,3,], [1,2,3], label='Series')
    custom_color = (1.0, 0, 0, 0.9)  # RGBa format
    legend = ax.legend(title='Title', facecolor=custom_color)

red legend background

see:

https://www.geeksforgeeks.org/change-legend-background-using-facecolor-in-matplotlib/

for defining custom color with alpha channel see:

https://matplotlib.org/stable/tutorials/colors/colors.html

maybe its possible to draw a rect above the legend, but it didnt work for me:

How do I draw a rectangle on the legend in matplotlib?

gorschel
  • 26
  • 8
0

Pyplot does allow you to set the background color (with the "facecolor" keyword), but it doesn't seem to allow you to set the alpha of the background (even using an (R,G,B,alpha) tuple).

To set the background color as red, use ax.legend(facecolor = 'red'). To set to a lighter color, one option is to specify an RGB tuple, e.g. with ax.legend(facecolor = (1,.5,.5)).

Sample code:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,], [1,2,3], label='Series')
ax.legend(facecolor = (1,.5,.5))

Result:

enter image description here


Edit: with framealpha:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,], [1,2,3], label='Series')
ax.legend(['Title'], facecolor = 'red', framealpha = 0.3)
Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16
  • 2
    Presumably OP wants only the background of the legend *title* to be red, not the entire legend. – BigBen Apr 25 '23 at 16:05
  • The [docs](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html) mention a `framealpha` keyword. – JohanC Apr 25 '23 at 16:08