seaborn:背景颜色的传说

mat*_*use 21 python plot legend seaborn

以下问题说明如何更改图例的背景颜色: 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()
Run Code Online (Sandbox Code Playgroud)

用matplotlib 与seaborn

mwa*_*kom 38

seaborn默认关闭图例框架,如果你想自定义框架的外观,我想你需要frameon=True在打电话时添加plt.legend.

  • 仍然奇怪的是,如果实际上没有帧,`frame = legend.get_frame()`会返回一些内容 (2认同)

rya*_*lon 10

set_style()方法可采取风格参数(例如'white','whitegrid','darkgrid'等等)和参数的字典来覆盖缺省值美学,包括是否有图例框架上或没有.

如果你想要改变其他一些小样式的东西,我经常这样做,你可以用这种方式一次性设置它们.

import seaborn
seaborn.set_style('darkgrid', {'legend.frameon':True})
Run Code Online (Sandbox Code Playgroud)

根据文档,你可以获得seaborn当前的rc设置seaborn.axes_style()

{'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}
Run Code Online (Sandbox Code Playgroud)