Seaborn 调色板不适用于线图

Seaborn Color Palette not working appropiate with lineplot

我在为线图自定义颜色时遇到了一些麻烦。我想用顺序调色板显示一组光谱。参数 "palette="blues" 工作正常,但不接受任何适当的颜色列表(如 "Blues_d"),其中不包含任何明亮的颜色。

下面你可以看到我正在使用的代码。

color = (sns.dark_palette("purple"))
sns.set()

ax = sns.lineplot(x="Wavelength", y="Absorption", hue="t (min)", lw=1, data=df1, palette=color, legend="brief")

问题是,我收到以下错误:

ValueError: The palette list has the wrong number of colors.

所以问题是:如何使用 lineplot 函数并使用蓝色、红色或任何不包含任何亮色的连续调色板?

我正在使用 pandas 版本 0.23.3、matplotlib 版本 2.2.2 和 seaborn 版本 0.9.0

由于您在 hue 选项中提到了 t (min) 列,因此您需要知道该列的唯一值总数。

假设列中有 5 个唯一值。因此,您可以将数字设置为 sns.color_palette:

n_colors 选项
ax = sns.lineplot(x="Wavelength", 
                  y="Absorption", 
                  hue="t (min)", 
                  lw=1, 
                  data=df1, 
                  palette=sns.color_palette('coolwarm', n_colors=5), 
                  legend="brief")

如果通过在 sns.color_palette 中将“as_cmap”参数设置为 True 将调色板设置为彩色地图,则无需担心颜色计数:

ax = sns.lineplot(x="Wavelength", 
                      y="Absorption", 
                      hue="t (min)", 
                      lw=1, 
                      data=df1, 
                      palette=sns.color_palette('coolwarm', as_cmap = True), 
                      legend="brief")
Palette = ["#090364", "#091e75"] #define your preference
sns.set_style("whitegrid")
sns.set_palette(Palette) #use the list defined in the function
plot = sns.countplot(df['Purchased']) #Enjoy ploting