Phyton matplotlib legend background color python matplotlib legend background color,matplotlib legend background color

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.

Answer:1

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.

Answer:2

In addition to Molly's method you can turn the frame off using the linewidth:

frame.set_linewidth(0)

I used that method in a small convenience function I wrote to hide the legend frames for the same reason you cite. The function is called adjust_legends in the print_targeted_plots module available from github.

Answer:3



  1. python matplotlib legend background color
  2. matplotlib legend background color

Suppose I have the following code: from scipy import * import multiprocessing as mp num_cores = mp.cpu_count() from joblib import Parallel, delayed import matplotlib.pyplot as plt def func(x,y): ...

Suppose I have the following code: from scipy import * import multiprocessing as mp num_cores = mp.cpu_count() from joblib import Parallel, delayed import matplotlib.pyplot as plt def func(x,y): ...

How can I format IPython html display of pandas dataframes so that numbers are right justified numbers have commas as thousands separator large floats have no decimal places I understand that numpy ...

How can I format IPython html display of pandas dataframes so that numbers are right justified numbers have commas as thousands separator large floats have no decimal places I understand that numpy ...