簡體   English   中英

seaborn:背景顏色的傳說

[英]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()

用matplotlib與seaborn

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM