使用color_palette()和set_palette()建立配色方案
使用set_palette()函数改变配色方案的默认设置
临时设置图表配色方案
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(rc={"figure.figsize": (6, 6)})
np.random.seed(sum(map(ord, "palettes")))
%matplotlib inline
HTML十六进制字符串(hex color codes)
合法的HTML颜色名字(HTML color names)
归一化到[0, 1]的RGB元组(RGB tuples)
只能用于Jupyter Notebook
sns.choose_colorbrewer_palette(data_type, as_cmap=False)
This describes the kind of data you want to visualize. Note that you can pass substrings(e.g. ‘q’ for ‘qualitative).
If True, the return value is a matplotlib colormap rather than a list of discrete colors.
sns.choose_colorbrewer_palette('sequential', as_cmap=False)
[(0.95755478915046244, 0.95755478915046244, 0.95755478915046244), (0.90120723387774304, 0.90120723387774304, 0.90120723387774304), (0.83289505032932054, 0.83289505032932054, 0.83289505032932054), (0.75021916137022127, 0.75021916137022127, 0.75021916137022127), (0.64341409276513495, 0.64341409276513495, 0.64341409276513495), (0.53871589525073182, 0.53871589525073182, 0.53871589525073182), (0.44032295626752516, 0.44032295626752516, 0.44032295626752516), (0.34288351570858677, 0.34288351570858677, 0.34288351570858677), (0.22329873945198808, 0.22329873945198808, 0.22329873945198808), (0.1046981975144031, 0.1046981975144031, 0.1046981975144031)]
当需要区分离散的数据集,且数据集之间没有内在的顺序,最好使用qualitative (or categorical) palettes
# 导入Seaborn的同时,会引入默认的颜色循环,由6种颜色构成
sns.color_palette() # 返回当前默认颜色循环
[(0.2980392156862745, 0.4470588235294118, 0.6901960784313725), (0.3333333333333333, 0.6588235294117647, 0.40784313725490196), (0.7686274509803922, 0.3058823529411765, 0.3215686274509804), (0.5058823529411764, 0.4470588235294118, 0.6980392156862745), (0.8, 0.7254901960784313, 0.4549019607843137), (0.39215686274509803, 0.7098039215686275, 0.803921568627451)]
# 显示当前默认颜色循环
current_palette = sns.color_palette()
sns.palplot(current_palette) # sns.palplot:plot the values in a color palette as a horizontal array
在默认颜色主题的基础上,有6个变种:deep、muted、pastel、bright、dark、colorblind
当需要区分的数据集超过颜色循环中的6种颜色时,最简单的方法是使用循环颜色系统
# 最为常用的方法是使用'hls'循环颜色系统
sns.palplot(sns.color_palette("hls", 8))
# 可以通过sns.hls_palette函数控制'hls'循环颜色系统的亮度和饱和度
sns.palplot(sns.hls_palette(8, l=.3, s=.8)) # l是亮度(lightness),s是饱和度(saturation)
# 'husl'循环颜色系统,在亮度和饱和度上分别更加平均
sns.palplot(sns.color_palette("husl", 8))
# 可以通过sns.husl_palette函数控制'husl'循环颜色系统的亮度和饱和度
sns.palplot(sns.husl_palette(8, l=.9, s=.9)) # l是亮度(lightness),s是饱和度(saturation)
sns.palplot(sns.color_palette("Paired"))
sns.palplot(sns.color_palette("Set2", 10))
flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
sns.palplot(sns.color_palette(flatui))
# 使用sns.xkcd_rgb设置颜色名字
plt.plot([0, 1], [0, 1], sns.xkcd_rgb["pale red"], lw=3)
plt.plot([0, 1], [0, 2], sns.xkcd_rgb["medium green"], lw=3)
plt.plot([0, 1], [0, 3], sns.xkcd_rgb["denim blue"], lw=3);
# 使用sns.xkcd_palette传入颜色名字列表
colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
sns.palplot(sns.xkcd_palette(colors))
当数据集的范围从相对低值(不感兴趣)到相对高值(很感兴趣),最好使用sequential color palettes
常用于kdeplot或corrplot等函数
The Color Brewer library中有大量的sequential color palettes,以占主导地位的颜色命名(如Blues)
sns.palplot(sns.color_palette("Blues"))
添加后缀_r,倒置sequential color palettes的顺序
sns.palplot(sns.color_palette("BuGn_r"))
添加后缀_d,使sequential color palettes颜色变深
sns.palplot(sns.color_palette("GnBu_d"))
cubehelix调色板系统的亮度是线性变化的,优点在于打印后也能区分不同颜色,且对色盲友好
sns.palplot(sns.color_palette("cubehelix", 8)) # 通过cubehelix参数调用
sns.palplot(sns.cubehelix_palette(8)) # 通过sns.cubehelix_palette()函数调用,与通过cubehelix参数调用的结果有所区别(色域的宽度变小、明暗变化倒置)
# start参数(取值0到3)和rot参数(number of rotations,任意值,取值-1到1)
sns.palplot(sns.cubehelix_palette(8, start=.5, rot=-.75))
# dark、light参数(控制endpoints的暗度和亮度)、reverse参数(倒置cubehelix_palette)
sns.palplot(sns.cubehelix_palette(8, start=2, rot=0, dark=0, light=.95, reverse=True))
# 默认返回颜色列表,设置as_cmap=True返回colormap对象,可以用于某些Seaborn或matplotlib函数
x, y = np.random.multivariate_normal([0, 0], [[1, -.5], [-.5, 1]], size=300).T
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
sns.kdeplot(x, y, cmap=cmap, shade=True)
<matplotlib.axes._subplots.AxesSubplot at 0x9b81630>
只能用于Jupyter Notebook
sns.choose_cubehelix_palette(as_cmap=False)
If True, the return value is a matplotlib colormap rather than a list of discrete colors.
sns.choose_cubehelix_palette(as_cmap=False)
[[0.9312692223325372, 0.8201921796082118, 0.7971480974663592], [0.8888663743660877, 0.7106793139856472, 0.7158661451411206], [0.8314793143949643, 0.5987041921652179, 0.6530062709235388], [0.7588951019517731, 0.49817117746394224, 0.6058723814510268], [0.6672565752652589, 0.40671838146419587, 0.5620016466433286], [0.5529215689527474, 0.3217924564263954, 0.5093718054521851], [0.43082755198027817, 0.24984535814964698, 0.4439396089963985], [0.29794615023641036, 0.18145907625614888, 0.35317781405034754], [0.1750865648952205, 0.11840023306916837, 0.24215989137836502]]
以某个颜色为种子,从明向暗或从暗向明渐变,产生sequential palettes
sns.palplot(sns.light_palette("green"))
sns.palplot(sns.dark_palette("purple"))
# 倒置调色板的顺序
sns.palplot(sns.light_palette("navy", reverse=True))
# 产生colormap对象,而非颜色列表
pal = sns.dark_palette("palegreen", as_cmap=True)
sns.kdeplot(x, y, cmap=pal);
# provide tuples in ``hls`` or ``husl`` space along with the default ``rgb``
sns.palplot(sns.light_palette((210, 90, 60), input="husl"))
# seed the palette with any valid ``xkcd`` color
sns.palplot(sns.dark_palette("muted purple", input="xkcd"))
只能用于Jupyter Notebook
sns.choose_light_palette(input='husl', as_cmap=False)
Color space for defining the seed value. Note that the default is different than the default input for light_palette().
If True, the return value is a matplotlib colormap rather than a list of discrete colors.
sns.choose_light_palette(input='husl', as_cmap=False)
[array([ 0.94054458, 0.95945542, 0.95679586, 1. ]), array([ 0.87363254, 0.90742758, 0.90267475, 1. ]), array([ 0.80672051, 0.85539974, 0.84855364, 1. ]), array([ 0.73741876, 0.80151376, 0.79249963, 1. ]), array([ 0.67050672, 0.74948591, 0.73837852, 1. ]), array([ 0.60120497, 0.69559993, 0.68232452, 1. ]), array([ 0.53429294, 0.64357209, 0.62820341, 1. ]), array([ 0.46499119, 0.58968611, 0.5721494 , 1. ]), array([ 0.39807915, 0.53765827, 0.51802829, 1. ]), array([ 0.33116712, 0.48563043, 0.46390718, 1. ])]
只能用于Jupyter Notebook
sns.choose_dark_palette(input='husl', as_cmap=False)
Color space for defining the seed value. Note that the default is different than the default input for dark_palette().
If True, the return value is a matplotlib colormap rather than a list of discrete colors.
sns.choose_dark_palette(input='husl', as_cmap=False)
[array([ 0.13333333, 0.13333333, 0.13333333, 1. ]), array([ 0.15505626, 0.17201694, 0.16963164, 1. ]), array([ 0.17677918, 0.21070054, 0.20592994, 1. ]), array([ 0.19927793, 0.2507657 , 0.24352462, 1. ]), array([ 0.22100085, 0.2894493 , 0.27982292, 1. ]), array([ 0.2434996 , 0.32951446, 0.31741759, 1. ]), array([ 0.26522252, 0.36819806, 0.3537159 , 1. ]), array([ 0.28772127, 0.40826322, 0.39131057, 1. ]), array([ 0.30944419, 0.44694683, 0.42760888, 1. ]), array([ 0.33116712, 0.48563043, 0.46390718, 1. ])]
当数据集的低值和高值都很重要,且数据集中有明确定义的中点时,最好使用diverging color palettes
例如,绘制温度相对于基准时间点的变化图,最好使用diverging colormap来同时显示温度相对于基准值的上升和下降
两端的颜色应该具有相似的亮度和饱和度,中间点的颜色不应该喧宾夺主
sns.palplot(sns.color_palette("BrBG", 7))
sns.palplot(sns.color_palette("RdBu_r", 7))
# coolwarm调色板也是很好的选择,缺点在于中间的颜色和两端的颜色对比较少
sns.palplot(sns.color_palette("coolwarm", 7))
diverging_palette()函数使用'husl'颜色系统,需要在函数中设置两个hue参数,也可以选择设置两端颜色的亮度和饱和度
sns.palplot(sns.diverging_palette(220, 20, n=7))
sns.palplot(sns.diverging_palette(145, 280, s=85, l=25, n=7))
# sep参数controls the width of the separation between the two ramps in the middle region of the palette
sns.palplot(sns.diverging_palette(10, 220, sep=80, n=7))
# 可以将中间点的颜色设置成暗色而非亮色
sns.palplot(sns.diverging_palette(255, 133, l=60, n=7, center="dark"))
只能用于Jupyter Notebook
sns.choose_diverging_palette(as_cmap=False)
If True, the return value is a matplotlib colormap rather than a list of discrete colors.
sns.choose_diverging_palette(as_cmap=False)
[array([ 0.25199714, 0.49873371, 0.57516028, 1. ]), array([ 0.43026136, 0.62000665, 0.67878019, 1. ]), array([ 0.60852558, 0.74127959, 0.7824001 , 1. ]), array([ 0.7867898 , 0.86255253, 0.88602001, 1. ]), array([ 0.95, 0.95, 0.95, 1. ]), array([ 0.95457726, 0.76653099, 0.78032569, 1. ]), array([ 0.91971827, 0.58735877, 0.61174 , 1. ]), array([ 0.88485928, 0.40818655, 0.44315432, 1. ]), array([ 0.85104086, 0.23436275, 0.27960104, 1. ])]
color_palette()与set_palette()的关系,类似于axes_style()和set_style()的关系
set_palette()的参数与color_palette()相同
区别在于,set_palette()会改变配色方案的默认设置,从而应用于之后所有的图表
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sns.set_palette("husl")
sinplot()
在with语句里使用color_palette()函数来临时设置图表配色方案
with sns.color_palette("PuBuGn_d"):
sinplot()