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()
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With