matplotlib legend background color

user1850133 :

Is there while rcParams['legend.frameon'] = 'False' a simple way to fill the legend area background with a given colour. More specifically I would like the grid not to be seen on the legend area because it disturbs the text reading.

The keyword framealpha sounds like what I need but it doesn't change anything.

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['legend.frameon'] = 'False'
plt.plot(range(5), label = u"line")
plt.grid(True)
plt.legend(loc = best)
plt.show()

I've also tried:

legend = plt.legend(frameon = 1)
frame = legend.get_frame()
frame.set_color('white')

but then I need to ask how can I change the background colour while keeping the frame on? Sometimes I want it ON with a background colour other than white. And also, is there a way of changing the colour of the frame? With the above code I was expecting to change the colour of the frame only, not the background.

Molly :

You can set the edge color and the face color separately like this:

frame.set_facecolor('green')
frame.set_edgecolor('red')

There's more information under FancyBboxPatch here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

seaborn: legend with background color

Matplotlib figure facecolor (background color)

Assign different background color for each legend in ggplot2

matplotlib conditional background color in python

mollview: use matplotlib colormaps and change background color

Set background color behind the image in matplotlib

Matplotlib adding legend based on existing color series

ggplot2: change the background color of one of the legend elements

How to fill legend background color when plotting with TWO axes?

matplotlib scatter plot with color label and legend specified by c option

How to add legend based on color in scatterplot [matplotlib]

Add background color to only one component of the legend in matplotlib

How to change the background color of matplotlib chart?

How to change background color in this custom-designed legend in ggplot?

Legend color not working with randomly generated background colors in chartjs pie chart

matplotlib legend based only on line style but not color

Set background color to matplotlib chart

Plotly: How to change background color of a plotly express legend?

Alternate grid background color in MatplotLib

Matplotlib scatter plot color-coded by text, how to add legend?

how to modify the legend and the background in matplotlib?

ggplot2: Change color of background for geom_vline legend

Matplotlib: combine legend with same color and name

matplotlib plotting legend corresponding to size and color of points

Matplotlib add color legend with value based on another variable

Python matplotlib set background rectangluar size of legend for grid spec

How can i give the full Legend a background color in chart js?

How to change background color of the title of legend

Multi-Color Legend Entry with Hatches in Matplotlib