• 图表可视化seaborn风格和调色盘


    seaborn是基于matplotlib的python数据可视化库,提供更高层次的API封装,包括一些高级图表可视化等工具。

    使用seaborn需要先安装改模块pip3 install seaborn 。

    一、风格style

    包括set() / set_style() / axes_style() / despine() / set_context()

    创建正弦函数并显示图表

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    %matplotlib inline
    def sinplot(flip = 1):
        x = np.linspace(0,14,100)
        for i in range(1,7):
            plt.plot(x,np.sin(x+i)*i)      # 6个正弦函数
    sinplot()

    1.set(),设置整体为默认风格

    sns.set()  #默认风格为darkgrid
    sinplot()

    2.set_style(),自定义整体风格

    参数为"white"、"dark"、 "whitegrid"、 "darkgrid"、 "ticks"或者None,默认为darkgrid

    fig = plt.figure(figsize=(15,6))
    
    ax1 = fig.add_subplot(121)                
    sns.set_style('whitegrid')
    data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
    sns.boxplot(data=data)
    plt.title('style - whitegrid')# 仍然可以使用matplotlib的参数
    
    ax2 = fig.add_subplot(122)    
    # sns.set_style("dark")
    sinplot()

    3.axes_style(),设置子图风格

    可与with搭配使用,设置with代码块内的图表风格,不影响整体图表风格。

    fig = plt.figure(figsize=(15,6))
    with sns.axes_style("dark"): #只对with代码块内的图表风格生效,即只对第一个子图生效
        plt.subplot(121)       
        sinplot()
    
    sns.set_style("white")       #整体风格为white
    plt.subplot(122)
    sinplot()

     

    4.despine()移除轴线

    despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False, offset=None, trim=False)

    top、right、left、bottom:上、右、左、下方轴线,默认移除上方和右侧轴线

    offset:xy轴和y轴的起点相对原始位置的偏移量

    trim:默认坐标轴长度没有限制,会延伸到图表内容结束,True表示将坐标轴的显示的长度在最小值和最大值之间

    fig = plt.figure(figsize=(20,6))
    ax1 = fig.add_subplot(131)  
    sinplot()
    sns.despine()# 默认删除上、右坐标轴
    
    ax2 = fig.add_subplot(132)
    sns.violinplot(data=data) #小提琴图
    # sns.despine(offset=1, trim=True) 
    
    ax3 = fig.add_subplot(133)
    sns.boxplot(data=data, palette="deep")
    sns.despine(left=True, right = False) #最终是该despine设置生效

    5.set_context()显示比例

    可选参数为'paper'、 'notebook'、'talk'、'poster',默认为notebook,设置标签、线等的大小。

    sns.set_context("notebook")
    sinplot()

    下面分别为设置为notebook、paper、talk和poster的显示结果。

    二、 调色盘

    1.color_palette()

    默认取当前调色盘的颜色,返回结果是一个seaborn.palette的类,形式类似一个列表,列表中每一个元素为元组,元组用3个数值表示rgb颜色。

    current_palette = sns.color_palette()  # 读取当前调色盘颜色,可添加参数n表示取几个色块
    print(current_pallette,type(current_palette))
    sns.palplot(current_palette)
    #<class 'seaborn.palettes._ColorPalette'> [(0.9913725490196079, 0.7913725490196079, 0.7082352941176471)...]

    seaborn可用调色盘有6种,deep、 muted、bright、 pastel、dark、colorblind,默认显示bright。

     

    其他调色盘

    sns.color_palette('Reds', 10),第一个参数表示色系,第二个参数表示取几个色块。

    颜色默认是由浅到深,带r表示反转即颜色由深到浅,不是所有颜色都可以翻转哦。

    #其他可用调色盘
    #Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, 
    #Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, 
    #Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r,RdBu, RdBu_r, 
    #RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, spectral
    #Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r
    #binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, 
    #cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, 
    #gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, 
    #gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, 
    #nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, 
    #seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, 
    #terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r
    
    sns.palplot(sns.color_palette('Reds', 10))
    sns.palplot(sns.color_palette('Greens_r', 7))  #

    # 分组颜色,同一个颜色成对出现
    sns.palplot(sns.color_palette('Paired',7))  #参数可以为奇数
    sns.palplot(sns.color_palette('Paired', 18))

    2.设置调色盘

    set_palette(palette, n_colors=None, desat=None, color_codes=False),使用Seaborn调色盘设置Matplotlib颜色循环

    palette参数可设置为seaborn color paltte | matplotlib colormap | hls | husl

    sns.set_palette('Greens')
    sinplot()

    3.亮度和饱和度

    sns.hls_palette(n_colors=6, h=.01, l=.6, s=.65)

    sns.husl_palette(n_colors=6, h=.01, s=.9, l=.65),两者表示亮度和饱和度的参数位置相反。

    参数n_colors表示取几个色块,h表示第一个色块的颜色,l表示亮度,s表示饱和度,h、l、s取值[0,1]。

    sns.palplot(sns.hls_palette(8,0.01,0.5,0.5))
    sns.palplot(sns.husl_palette(8,0.03,0.8,0.8))

    4.按线性增长设置颜色

    cubehelix_palette(n_colors=6, start=0, rot=.4, gamma=1.0, hue=0.8,light=.85, dark=.15, reverse=False, as_cmap=False)

    c_colors:色块个数

    start:色块的起点颜色,[0,3]之间

    rot:颜色的旋转角度

    gamma:颜色的伽马值,值越大颜色越深

    hue:饱和度,[0,1]之间

    light和dark:亮度和深度,[0,1]之间

    reverse:默认为False颜色由浅到深,True表示由深到浅

    as_cmp:If True, return a matplotlib colormap instead of a list of colors

    sns.palplot(sns.cubehelix_palette(8, gamma=1.5)) 
    sns.palplot(sns.cubehelix_palette(8, start=1, rot=-0.75)) 
    sns.palplot(sns.cubehelix_palette(8, start=2, rot=0, dark=0.5,light=0.9, reverse=True))

    5.按颜色深浅设置颜色

    light_palette(color, n_colors=6, reverse=False, as_cmap=False,input="rgb")和dark_palette(color, n_colors=6, reverse=False, as_cmap=False, input="rgb")

    color_palette()中的颜色参数为调色盘,而light_palette()和dark_palette()中的color颜色参数就是单纯的颜色,例如对于蓝色,color_palette()需设置Blues,后两者参数为blue。

    sns.palplot(sns.light_palette("red")) 
    sns.palplot(sns.dark_palette("red")) 
    sns.palplot(sns.light_palette("blue")) 
    sns.palplot(sns.dark_palette("blue", reverse=True)) 

    6.设置分散颜色

    diverging_palette(h_neg, h_pos, s=75, l=50, sep=10, n=6, center="light", as_cmap=False)

    h_neg, h_pos:起始和终止颜色,[0,359]之间

    s、l:饱和度和亮度,[0,100]之间

    n:色块个数

    center: 最中间颜色为浅色或者深色,{'light','dark'},默认为浅色

    sns.palplot(sns.diverging_palette(0,150, s=60, l=20, n=8))
    sns.palplot(sns.diverging_palette(300, 150, s=30, l=50, n=8,center='dark')) 

    sns.set_style("white")# 设置风格
    fig = plt.figure(figsize=(18,5))
    
    with sns.color_palette("Greens"): #设置局部调色盘
        plt.subplot(131)
        sinplot()
    
    sns.set_palette("husl")   #对于多系列的图表,用不同颜色区分系列
    plt.subplot(132)
    sinplot()
    
    x = np.arange(25).reshape(5, 5) 
    cmap = sns.diverging_palette(200, 20, sep=20, as_cmap=True) 
    plt.subplot(133)
    sns.heatmap(x, cmap=cmap)  #显示热力图效果

  • 相关阅读:
    输出python的help结果到文件中
    webdriver 的三种等待方式
    Loadrunner 怎么将response的数据下载下来
    Loadrunner web_reg_find 和web_reg_save_param 比较
    LR的响应时间与使用IE所感受时间不一致的讨论
    Loadrunner错误-26601、-27492、-27727处理方法
    loadrunner运行乱码解决方法
    OpenGL ES: (1) OpenGL ES的由来 (转)
    JPG:文件格式系列科普之.JPEG/.JPG(转)
    单色位图、16色位图、256色位图的含义
  • 原文地址:https://www.cnblogs.com/Forever77/p/11396588.html
Copyright © 2020-2023  润新知