19

我在 IPython Notebook 中使用了奇妙的Seaborn库来获取一些摘要统计信息。我最近为我的笔记本切换到了深色主题,并试图为 Seaborn 找出深色背景的最佳视觉效果。我正在使用该darkgrid样式,但图例仍以黑色打印,这使得它们无法阅读。这是一个例子:

深色背景上的 Seaborn

修复我的 Seaborn 使用风格以使图例显示为白色的最佳方法是什么?

更新:我刚刚注意到我的 Matplotlib 图存在同样的问题。所以我的问题更笼统。您使用什么样式来允许在深色图上使用白色以便图例可读?

4

4 回答 4

20

您可以自定义 seaborn 样式,并且它会尝试使其相对容易做到这一点。

如果您想查看被视为“样式”定义一部分的每个参数,只需sns.axes_style()不带参数调用,它将返回当前设置。从 0.3.1 开始,对于默认样式(“darkgrid”),如下所示:

{'axes.axisbelow': True,
 'axes.edgecolor': 'white',
 'axes.facecolor': '#EAEAF2',
 'axes.grid': True,
 'axes.labelcolor': '.15',
 'axes.linewidth': 0,
 'font.family': 'Arial',
 'grid.color': 'white',
 'grid.linestyle': '-',
 'image.cmap': 'Greys',
 'legend.frameon': False,
 'legend.numpoints': 1,
 'legend.scatterpoints': 1,
 'lines.solid_capstyle': 'round',
 'pdf.fonttype': 42,
 'text.color': '.15',
 'xtick.color': '.15',
 'xtick.direction': 'out',
 'xtick.major.size': 0,
 'xtick.minor.size': 0,
 'ytick.color': '.15',
 'ytick.direction': 'out',
 'ytick.major.size': 0,
 'ytick.minor.size': 0}

一个很好的启发式方法是您可能只需要"color"名称中的参数,因此您可以对其进行过滤:

{k: v for k, v in sns.axes_style().items() if "color" in k}

返回

{'axes.edgecolor': 'white',
 'axes.facecolor': '#EAEAF2',
 'axes.labelcolor': '.15',
 'grid.color': 'white',
 'text.color': '.15',
 'xtick.color': '.15',
 'ytick.color': '.15'}

然后,您可以将包含这些参数值的自定义字典传递到sns.set_style()

custom_style = {'axes.labelcolor': 'white',
                'xtick.color': 'white',
                'ytick.color': 'white'}
sns.set_style("darkgrid", rc=custom_style)
于 2014-08-22T16:52:15.413 回答
9
sns.set_style("darkgrid")

将使背景变白,以便您可以看到文本。

在此处输入图像描述

于 2015-11-24T13:28:56.543 回答
6

为什么不简单

plt.style.use("dark_background")

于 2018-12-06T13:20:11.643 回答
5

I find that adding this

plt.figure(facecolor='w')

each time I plot takes care of the axes background.

于 2015-03-06T21:49:26.547 回答