[英]seaborn: legend with background color
以下問題說明如何更改圖例的背景顏色 : matplotlib圖例背景顏色 。 但是,如果我使用seaborn,這不起作用。 有沒有辦法做到這一點?
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()
seaborn默認關閉圖例框架,如果你想自定義框架的外觀,我想你在調用plt.legend
時需要添加frameon=True
。
set_style()
方法可以采用樣式參數(例如'white'
, 'whitegrid'
, 'darkgrid'
等)和參數的dict來覆蓋默認的美學,包括是否有圖例框架。
如果你想要改變其他一些小樣式的東西,我經常這樣做,你可以用這種方式一次性設置它們。
import seaborn
seaborn.set_style('darkgrid', {'legend.frameon':True})
根據文檔 ,您可以使用seaborn.axes_style()
獲取seaborn
當前的rc
設置
{'axes.axisbelow': True,
'axes.edgecolor': '.8',
'axes.facecolor': 'white',
'axes.grid': True,
'axes.labelcolor': '.15',
'axes.linewidth': 1.0,
'figure.facecolor': 'white',
'font.family': [u'sans-serif'],
'font.sans-serif': [u'Arial',
u'DejaVu Sans',
u'Liberation Sans',
u'Bitstream Vera Sans',
u'sans-serif'],
'grid.color': '.8',
'grid.linestyle': u'-',
'image.cmap': u'rocket',
'legend.frameon': False,
'legend.numpoints': 1,
'legend.scatterpoints': 1,
'lines.solid_capstyle': u'round',
'text.color': '.15',
'xtick.color': '.15',
'xtick.direction': u'out',
'xtick.major.size': 0.0,
'xtick.minor.size': 0.0,
'ytick.color': '.15',
'ytick.direction': u'out',
'ytick.major.size': 0.0,
'ytick.minor.size': 0.0}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.