【问题标题】:Custom shade palette in kdeplot seabornkdeplot seaborn 中的自定义阴影调色板
【发布时间】:2021-05-03 14:11:33
【问题描述】:

我正在尝试在我的 kdeplot 中自定义阴影颜色。

我想使用这个调色板,从浅绿色到#3bd6b0 颜色至少有 15 个级别(但以后可能会更多)。为了做到这一点,我像这样使用sns.light_palette

sns.light_palette('#3bd6b0',15)

这很好用并且对应这个调色板:

但是当我运行这段代码时:

sns.set_palette(sns.light_palette('#3bd6b0', 15))

x = np.random.normal(10, 5, 20)
y = np.random.normal(10, 5, 20)

sns.kdeplot( x=x, y=y, shade=True, shade_lowest=False,cbar=True, thresh= 0.0001, n_levels=15)

我明白了:

这不是我想要的...
我用参数cmappalette 尝试了很多东西,但没有任何效果。

你能帮我看看我的代码有什么问题吗?

非常感谢

【问题讨论】:

    标签: python seaborn data-visualization


    【解决方案1】:

    kdeplot 可以根据给定的固定颜色创建自己的阴影,或者您可以显式设置颜色图。如果您什么都不提供,则使用 matplotlib 的颜色循环。在您的情况下,kdeplot 将从遇到的第一种颜色中的第一种(这是调色板中最亮的颜色)创建自己的色带。如果给定颜色被认为是浅色,则色带会变暗,而当给定颜色被认为是暗色时,色带会变暗。

    您可以通过sns.kdeplot(..., cmap=...) 提供颜色图。 sns.colorpalette(..., as_cmap=True) 将 seaborn 调色板转换为颜色图。

    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    
    x = np.random.normal(10, 5, 20)
    y = np.random.normal(10, 5, 20)
    
    cmap = sns.light_palette('#3bd6b0', as_cmap=True)
    ax = sns.kdeplot(x=x, y=y, shade=True, cbar=True, thresh=0.0001, n_levels=15, cmap=cmap)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多