Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Seaborn调色板中选择颜色_Python_Seaborn - Fatal编程技术网

Python 从Seaborn调色板中选择颜色

Python 从Seaborn调色板中选择颜色,python,seaborn,Python,Seaborn,我有五个并排的分布图,通常使用color属性更改每个分布图的颜色。但是,现在我想使用Seaborn的husl调色板,我不知道如何将调色板中的颜色应用到每个图表。我很确定这只是我看得太多了 # sns.set(style="white", palette="muted", color_codes=True) sns.set(style="white", palette="husl", color_codes=True) # Set up the matplotlib figure f, ax

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

# 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通过提供与husl空间的接口。您可以创建一个调色板,根据您的独特类别(此处为“评级”)使用尽可能多的颜色。然后,为调色板编制索引或对其进行迭代。后者如下所示

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()

啊,太简单了!对于像我这样希望为每个图表指定颜色的其他人,我是这样做的。使用
palete=sns.husl_调色板(len(ratings))
设置调色板,然后在每个绘图中使用
color=palete[#]
其中#是要使用的索引号。