为什么 Seaborn 条形图会降低颜色的饱和度? [英] Why is Seaborn barplot desaturating colors?

查看:94
本文介绍了为什么 Seaborn 条形图会降低颜色的饱和度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用几个不同的库(bokehseabornmatlotlib)在 Python 中绘制绘图,但保持相同的配色方案.我从散景中选择了分类调色板:
从 bokeh.palettes 导入 Category10 作为调色板
然后也在 seabornmatplotlib 中使用它.我的问题是,尽管 matplotlib 的颜色看起来与 bokeh(在调色板中定义)非常相似,但 seaborn 以某种方式显示出明显的深色(即不饱和或不饱和)比它应该的.我想知道它是否在默认情况下对任何配色方案进行某种调暗,以及是否有任何方法可以避免这种情况.下面是使用不同库制作相同条形图的代码
使用 bokeh:

I am trying to make plots in Python using several different libraries (bokeh, seaborn and matlotlib), but keeping the same color scheme. I have chosen categorical pallete from bokeh with:
from bokeh.palettes import Category10 as palette
and then also used it in seaborn and matplotlib. My problem is that, although in matplotlib color seem to very similar to bokeh (as defined in the palette), seaborn shows somehow noticeable darker colors (i.e. less saturated or desaturated) than it should be. I am wondering if it is making some kind of dimming of any color scheme by default, and if there is any way to avoid this. Below there is code for making the same barplot using different libraries
Using bokeh:

source = pd.DataFrame({'names': ['exp_1', 'exp_2'], 'data':[3, 5], 'color':palette[10][:2]})
p = bokeh.plotting.figure(x_range=['exp_1', 'exp_2'], y_range=(0,6), plot_height=500, title="test")
p.vbar(x='names', top='data', width=0.9,  legend_field="names", source=source, color='color')
p.xgrid.grid_line_color = None
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
p.xaxis.major_label_text_font_size = '22pt'
p.yaxis.major_label_text_font_size = '22pt'
bokeh.io.show(p)

使用matplotlib:

# same palette both for seaborn and matplotlib (taken from bokeh palette)
sns_palette=sns.color_palette(palette[10]) 
fig, ax = plt.subplots()
plt.style.use('seaborn')
ax.set_xlabel('experiment', fontsize=20)
ax.tick_params(axis='both', which='major', labelsize=22)
ax.set_xticks([0, 1])
ax.set_xticklabels(['exp_1', 'exp_2'], fontsize=18)
ax.bar([0, 1], source['data'], align='center', color=sns_palette[:2])

并使用bokeh:

plt.figure()
ax = sns.barplot(x="names", y="data", data=source, palette=sns_palette[0:2])
ax.set_xlabel('experiment', fontsize=20)
ax.tick_params(axis='both', which='major', labelsize=18)
plt.tight_layout()


散景条形图:

matplotlib 条形图

seaborn barplot:


bokeh barplot:

matplotlib barplot

seaborn barplot:

推荐答案

Seaborn barplot 将条形面颜色的饱和度默认设置为 0.75.这可以通过将 saturation=1 添加到 barplot 调用来覆盖.

Seaborn barplot sets the saturation of the bar face colors to 0.75 by default. This can be overridden by adding saturation=1 to the barplot call.

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

source = pd.DataFrame({'names': ['exp_1', 'exp_2'], 'data':[3, 5]})
fig, ax = plt.subplots(1, 2)

# default saruration setting
sns.barplot(x="names", y="data", data=source, ax=ax[0])
ax[0].set_title('default saturation')

# additional parameter `saturation=1` passed to barplot
sns.barplot(x="names", y="data", data=source, saturation=1, ax=ax[1])
ax[1].set_title('saturation=1')

(这个答案直接来自@JohanC 的评论,我只是将它提升为一个答案......很高兴拥有该用户.)

(This answer is straight form the comment by @JohanC, I'm just elevating it to an answer ... happy for ownership to go to that user.)

这篇关于为什么 Seaborn 条形图会降低颜色的饱和度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆