【问题标题】:Seaborn chart colors are different than those specified by paletteSeaborn 图表颜色与调色板指定的颜色不同
【发布时间】:2017-11-04 05:11:05
【问题描述】:

为什么 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))

条形图

调色板图

【问题讨论】:

    标签: matplotlib colors seaborn palette


    【解决方案1】:

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

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

    ax = sns.countplot(x="class", data=titanic, palette=sns.xkcd_palette(colors), saturation=1)
    sns.palplot(sns.xkcd_palette(colors))
    

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

    【讨论】:

    • 正确答案,虽然我不会说“大多数”都是正确的,只是那些绘制大块的那些(如果它们不那么亮,通常看起来更好)。
    • @mwaskom 有没有办法将饱和度默认设置为 1?使用seaborn.apionly 时不受影响。而且似乎没有sns.set(style="white", saturation=1) 这样的东西。一般来说,我认为将样式固定到实际的绘图命令是不好的样式。
    • 不,seaborn 没有定义任何 matplotlib 没有的全局变量/默认值。
    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2018-05-04
    • 2016-01-04
    相关资源
    最近更新 更多