我已经有一个我使用的自定义 Seaborn 调色板,例如
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)
有没有办法让整个调色板变暗或变亮?但保留映射
您可以将颜色转换为 hsv 颜色模型,并将
value
(亮度)更改为更接近 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()