从 Seaborn 调色板中选择颜色 [英] Selecting colors from Seaborn palette

查看:106
本文介绍了从 Seaborn 调色板中选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个并排分布图,通常使用 color 属性更改每个分布图的颜色.但是,现在我想使用 Seaborn 的 husl 调色板,但我不知道如何将调色板中的颜色应用于每个图表.我很确定这只是我刚刚在看的东西.

# sns.set(style="white", Palette="muted", color_codes=True)sns.set(style="white", 调色板="husl", color_codes=True)# 设置matplotlib图形f,轴 = plt.subplots(ncols=5, figsize=(15, 4))sns.despine(左=真)# 评分为 1sns.distplot(df1[df1['rating']==1]['cost'], kde=False, color='c', ax=axes[0], axlabel="Rating of 1")# 评分为 2sns.distplot(df1[df1['rating']==2]['cost'], kde=False, color='k', ax=axes[1], axlabel='Rating of 2')# 评分为 3sns.distplot(df1[df1['rating']==3]['cost'], kde=False, color="g", ax=axes[2], axlabel='Rating of 3')# 评分为 4sns.distplot(df1[df1['rating']==4]['cost'], kde=False, color="m", ax=axes[3], axlabel='Rating of 4')# 评分为 5sns.distplot(df1[df1['rating']==5]['cost'], kde=False, color="b", ax=axes[4], axlabel='Rating of 5')plt.setp(轴,yticks=[])plt.tight_layout()

解决方案

Seaborn 通过

I have five distribution plots side by side and usually changed the color of each one using the color property. However, now I want to use Seaborn's husl palette and I can't figure out how to apply a color from the palette to each chart. I'm pretty sure this is something simply I'm just over looking.

# sns.set(style="white", palette="muted", color_codes=True)  
sns.set(style="white", palette="husl", color_codes=True)

# Set up the matplotlib figure
f, axes = plt.subplots(ncols=5, figsize=(15, 4))
sns.despine(left=True)

# Rating of 1
sns.distplot(df1[df1['rating']==1]['cost'], kde=False, color='c', ax=axes[0], axlabel="Rating of 1")

# Rating of 2
sns.distplot(df1[df1['rating']==2]['cost'], kde=False, color='k', ax=axes[1], axlabel='Rating of 2')

# Rating of 3
sns.distplot(df1[df1['rating']==3]['cost'], kde=False, color="g", ax=axes[2], axlabel='Rating of 3')

# Rating of 4
sns.distplot(df1[df1['rating']==4]['cost'], kde=False, color="m", ax=axes[3], axlabel='Rating of 4')

# Rating of 5
sns.distplot(df1[df1['rating']==5]['cost'], kde=False, color="b", ax=axes[4], axlabel='Rating of 5')

plt.setp(axes, yticks=[])
plt.tight_layout()

解决方案

Seaborn provides an interface to the husl space via husl_palette. You may create a palette with as many colors as you have unique categories (here, "ratings"). Then, either index the palette or iterate over it. The latter is shown below.

import matplotlib.pyplot as plt
import seaborn as sns; sns.set(style="white")
import pandas as pd
import numpy as np

df = pd.DataFrame({"cost" : np.random.randn(600),
                   "rating" : np.random.choice(np.arange(1,6), size=600)})

ratings = np.unique(df.rating.values)
palette = iter(sns.husl_palette(len(ratings)))

f, axes = plt.subplots(ncols=len(ratings), figsize=(15, 4))
sns.despine(left=True)

for (n, rat), ax in zip(df.groupby("rating"), axes):

    sns.distplot(rat["cost"], kde=False, color=next(palette), ax=ax, axlabel=f"Rating of {n}")

plt.setp(axes, yticks=[])
plt.tight_layout()
plt.show()

这篇关于从 Seaborn 调色板中选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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