Seaborn 图表颜色与调色板指定的颜色不同

Bir*_*Law 8 colors matplotlib palette seaborn

为什么 seaborn 图表颜色与调色板指定的颜色不同?

以下两个图表显示了条形图上显示的颜色与调色板图中显示的颜色之间的差异。如果您仔细观察,您会发现条形图上的颜色亮度/饱和度略低。

为什么这些不同,我怎样才能让条形图的颜色与调色板中指定的颜色完全相同?

import seaborn as sns
sns.set(style="white")
titanic = sns.load_dataset("titanic")

colors = ["windows blue", "amber", "greyish", "faded green", "dusty 
purple"]

ax = sns.countplot(x="class", data=titanic, 
palette=sns.xkcd_palette(colors))
sns.palplot(sns.xkcd_palette(colors))
Run Code Online (Sandbox Code Playgroud)

条形图
条形图

调色板图
调色板图

Imp*_*est 8

许多 seaborn 绘图命令都有一个参数saturation,其默认值为0.75. 它将 HSL 颜色空间(范围从 0 到 1)中颜色的饱和度 (S) 设置为给定值。

1在 countplot 中将此参数设置为将在两个图中为您提供相同的颜色。

ax = sns.countplot(x="class", data=titanic, palette=sns.xkcd_palette(colors), saturation=1)
sns.palplot(sns.xkcd_palette(colors))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这种默认去饱和的原因是许多人认为对比度较小的情节更有吸引力。这也是为什么seaborn的默认背景不是白色而是蓝灰色的原因。毕竟,这当然是一个品味问题。