seaborn

Make Seaborn color palette darker


I already have a custom Seaborn color palette that I use, e.g.

pkmn_type_colors_palette = {
    'Grass': '#78C850',
    'Fire': '#F08030',
    'Water': '#6890F0',
    'Bug': '#A8B820',
    'Normal': '#A8A878',
    'Poison': '#A040A0',
    'Electric': '#F8D030',
    'Ground': '#E0C068',
    'Fairy': '#EE99AC',
    'Fighting': '#C03028',
    'Psychic': '#F85888',
    'Rock': '#B8A038',
    'Ghost': '#705898',
    'Ice': '#98D8D8',
    'Dragon': '#7038F8',
    'Dark': sns.xkcd_rgb['dark indigo'],
    'Flying': sns.xkcd_rgb['sky'],
    'Steel': sns.xkcd_rgb['steel'],
}


sns.violinplot(x='Type1', hue='Type1', y='Defense', data=poke_df, palette=pkmn_type_colors_palette)

which gives a plot like
Violin Plot

is there a way to make the whole color palette darker or lighter? but keep the mappings


Solution

  • You could convert the color to the hsv color model and change the value (brightness) closer to 0:

    import matplotlib.pyplot as plt
    from matplotlib.colors import rgb_to_hsv, hsv_to_rgb, to_rgb
    import seaborn as sns
    import pandas as pd
    import numpy as np
    
    def darken(color):
        hue, saturation, value = rgb_to_hsv(to_rgb(color))
        return hsv_to_rgb((hue, saturation, value * 0.6))
    
    pkmn_type_colors_palette = {
        'Grass': '#78C850',
        'Fire': '#F08030',
        'Water': '#6890F0',
        'Bug': '#A8B820',
        'Normal': '#A8A878',
        'Poison': '#A040A0',
        'Electric': '#F8D030',
        'Ground': '#E0C068',
        'Fairy': '#EE99AC',
        'Fighting': '#C03028',
        'Psychic': '#F85888',
        'Rock': '#B8A038',
        'Ghost': '#705898',
        'Ice': '#98D8D8',
        'Dragon': '#7038F8',
        'Dark': sns.xkcd_rgb['dark indigo'],
        'Flying': sns.xkcd_rgb['sky'],
        'Steel': sns.xkcd_rgb['steel']
    }
    
    poke_df = pd.DataFrame({'Type1': np.repeat(list(pkmn_type_colors_palette), 20)})
    poke_df['Defense'] = np.random.randn(len(poke_df)).cumsum()
    plt.figure(figsize=(11, 4))
    dark_palette = {key: darken(pkmn_type_colors_palette[key]) for key in pkmn_type_colors_palette}
    sns.violinplot(x='Type1', hue='Type1', y='Defense', data=poke_df, palette=dark_palette)
    plt.tight_layout()
    plt.show()
    

    darkening a seaborn palette