• sns.barplot() 画条形图


    sns.barplot() :条形图主要展现的是每个矩形高度的数值变量的中心趋势的估计

    条形图只显示平均值(或其他估计值)

    注:countplot参数和barplot基本差不多,可以对比着记忆,有一点不同的是countplot中不能同时输入x和y,且countplot没有误差棒,

    类别特征barplot要同时传入x,y

    seaborn.barplot(x=None, y=None, hue=None, 
                    data=None, order=None, hue_order=None, 
                    estimator=<function mean>, ci=95, 
                    n_boot=1000, units=None, orient=None, 
                    color=None, palette=None, saturation=0.75, 
                    errcolor='.26', errwidth=None, capsize=None, 
                    dodge=True, ax=None, **kwargs)

    参数说明

    x,y,hue:数据字段变量名(如上表,date,name,age,sex为数据字段变量名)
    
    data: DataFrame,数组或数组列表
    
    order,hue_order:字符串列表
    作用:显式指定分类顺序,eg. order=[字段变量名1,字段变量名2,...]
    
    estimator:可回调函数
    作用:设置每个分类箱的统计函数
    
    ci:float或者"sd"或None
    在估计值附近绘制置信区间的大小,如果是"sd",
    则跳过bootstrapping并绘制观察的标准差,
    如果为None,则不执行bootstrapping,并且不绘制错误条。
    
    n_boot:int
    计算置信区间时使用的引导迭代次数
    
    orient: v | h
    图的显示方向(垂直或水平,即横向或纵向),
    这通常可以从输入变量的dtype推断得到
    
    color:matplotlib颜色
    
    palette:调试板名称,列表或字典类型
    作用:设置hue指定的变量的不同级别颜色。
    
    saturation 饱和度:float
    
    errcolor : matplotlib color
    作用:表示置信区间的线条颜色
    
    errfloat
    作用:表示误差线的厚度
    
    capsize:float
    作用:表示误差线上""的宽度(误差线上的横线的宽度)
    
    dodge:bool
    作用:使用色调嵌套时,是否应沿分类轴移动元素。

    同样使用泰坦尼克号数据作为例子

    #模拟生成数据集的方法
    
    %matplotlib inline
    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt 
    plt.rc("font",family="SimHei",size="12")  #用于解决中文显示不了的问题
    sns.set_style("whitegrid") 
    
    boolean=[True,False]
    gender=['','']
    color=['green','blue','yellow']
    data=pd.DataFrame({'height':np.random.randint(150,190,100),
    'weight':np.random.randint(40,90,100),
    'smoker':[boolean[x] for x in np.random.randint(0,2,100)],
    'gender':[gender[x] for x in np.random.randint(0,2,100)],
    'age':np.random.randint(15,90,100),
    'color':[color[x] for x in np.random.randint(0,len(color),100)]})
    
    
    #x,y要同时出现
    sns.barplot(x="color",y="age",data=data)
    
    #或者直接使用df[col]
    sns.barplot(x=data["color"],y=data["age"])

    #hue:根据hue列分类
    sns.barplot(x="color",y="age",data=data,hue="gender")

    #order, hue_order (lists of strings):用于控制条形图的顺序
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="gender",y="age",data=data,ax=axes[0])
    sns.barplot(x="gender",y="age",data=data,ax=axes[1],order=["",""])

    #estimator=(function name)控制条形图的取整列数据的什么值
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="gender",y="age",data=data,ax=axes[0])  #左图,默认为平均值
    sns.barplot(x="gender",y="age",estimator=np.median,data=data,ax=axes[1])  #右图,中位数

    #ci(float):统计学上的置信区间(在0-100之间),若填写"sd",则误差棒用标准误差。(默认为95)
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="color",y="age",data=data,ci=0,ax=axes[0])  #左图
    sns.barplot(x="color",y="age",data=data,ci="sd",ax=axes[1])  #右图

    #capsize(float): 设置误差棒帽条(上下两根横线)的宽度
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="color",y="age",data=data,ax=axes[0],capsize=.2)  #左图
    sns.barplot(x="color",y="age",data=data,ax=axes[1],capsize=.9)  #右图

    #palette:调色板,控制不同的颜色style
    fig,axes=plt.subplots(2,1)
    sns.barplot(x="color",y="age",data=data,ax=axes[0])  #上图
    sns.barplot(x="color",y="age",data=data,palette="Set3",ax=axes[1])  #下图

    #X,Y轴互换
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="age",y="color",data=data,ax=axes[0])  #左图
    sns.barplot(x="color",y="age",data=data,ax=axes[1])  #右图

     全部代码如下

    # -*- coding: utf-8 -*-
    """
    Created on Tue Jul 21 10:52:00 2020
    
    @author: Admin
    """
    
    
    #模拟生成数据集的方法
    
    %matplotlib inline
    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt 
    plt.rc("font",family="SimHei",size="12")  #用于解决中文显示不了的问题
    sns.set_style("whitegrid") 
    
    boolean=[True,False]
    gender=['','']
    color=['green','blue','yellow']
    data=pd.DataFrame({'height':np.random.randint(150,190,100),
    'weight':np.random.randint(40,90,100),
    'smoker':[boolean[x] for x in np.random.randint(0,2,100)],
    'gender':[gender[x] for x in np.random.randint(0,2,100)],
    'age':np.random.randint(15,90,100),
    'color':[color[x] for x in np.random.randint(0,len(color),100)]})
    
    
    #x,y要同时出现
    sns.barplot(x="color",y="age",data=data)
    
    #或者直接使用df[col]
    sns.barplot(x=data["color"],y=data["age"])
    
    
    #hue:根据hue列分类
    sns.barplot(x="color",y="age",data=data,hue="gender")
    
    
    #order, hue_order (lists of strings):用于控制条形图的顺序
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="gender",y="age",data=data,ax=axes[0])
    sns.barplot(x="gender",y="age",data=data,ax=axes[1],order=["",""])
    
    #estimator=(function name)控制条形图的取整列数据的什么值
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="gender",y="age",data=data,ax=axes[0])  #左图,默认为平均值
    sns.barplot(x="gender",y="age",estimator=np.median,data=data,ax=axes[1])  #右图,中位数
    
    
    #ci(float):统计学上的置信区间(在0-100之间),若填写"sd",则误差棒用标准误差。(默认为95)
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="color",y="age",data=data,ci=0,ax=axes[0])  #左图
    sns.barplot(x="color",y="age",data=data,ci="sd",ax=axes[1])  #右图
    
    
    #capsize(float): 设置误差棒帽条(上下两根横线)的宽度
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="color",y="age",data=data,ax=axes[0],capsize=.2)  #左图
    sns.barplot(x="color",y="age",data=data,ax=axes[1],capsize=.9)  #右图
    
    #palette:调色板,控制不同的颜色style
    fig,axes=plt.subplots(2,1)
    sns.barplot(x="color",y="age",data=data,ax=axes[0])  #上图
    sns.barplot(x="color",y="age",data=data,palette="Set3",ax=axes[1])  #下图
    
    
    #X,Y轴互换
    fig,axes=plt.subplots(1,2)
    sns.barplot(x="age",y="color",data=data,ax=axes[0])  #左图
    sns.barplot(x="color",y="age",data=data,ax=axes[1])  #右图
  • 相关阅读:
    windows server2016设置关闭自动更新
    ubuntu20/mac 安装php8.0
    Linux / Python 打印花式字符串
    Linux 增加 DNS 域名解析服务器
    挺不错的一个开源国产上线部署平台:walle
    Python 定时任务实现只执行一次的方法
    JavaScript 正则入门
    grid布局详解
    flex布局详解
    CSS3 入门指南(二)
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/13356745.html
Copyright © 2020-2023  润新知