Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seaborn: legend with background color

Tags:

The following question explains how to change the background color of a legend: matplotlib legend background color. However, if I use seaborn this does not work. Is there a way to do this?

import matplotlib.pyplot as plt import numpy as np a = np.random.rand(10,1)  plt.plot(a, label='label') legend = plt.legend() frame = legend.get_frame() frame.set_facecolor('green') plt.show()   import seaborn as sns  plt.plot(a, label='label') legend = plt.legend() frame = legend.get_frame() frame.set_facecolor('green') plt.show() 

with matplotlibwith seaborn

like image 486
mathause Avatar asked Jun 09 '15 10:06

mathause


People also ask

How do I change the background color in Seaborn?

Use the seaborn. set() Function to Change the Background Color of Seaborn Plots in Python. The set() function adds different elements and configures the aesthetics of the plot. There is no direct argument or method to change background color in seaborn.

How do you customize your legend in Seaborn?

To change the position of a legend in a seaborn plot, you can use the plt. legend() command. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do I change the legend color in Matplotlib?

To place the legend, use legend() method with location of the legend and store the returned value to set the color of the text. To set the color of the text, use set_color() method with green color.

How do you make a legend outside the plot in Seaborn?

You can use the bbox_to_anchor() argument to place a seaborn legend outside of the plot. Note that the (1.05, 1) coordinates correspond to the (x, y) coordinates where the legend should be placed and the borderaxespad specifies the padding between the axes and the border legend.


1 Answers

seaborn turns the legend frame off by default, if you want to customize how the frame looks, I think you'll need to add frameon=True when you call plt.legend.

like image 88
mwaskom Avatar answered Sep 29 '22 04:09

mwaskom