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
作用:表示置信區間的線條顏色

errwidth:float
作用:表示誤差線的厚度

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])  #右圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM