sns.countplot() 用於類別特征的頻數條形圖,可以畫類別特征和y值(y值特征也是類比的話)的條形圖
sns.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)
參數說明:
x: x軸上的條形圖,以x標簽划分統計個數
y:y軸上的條形圖,以y標簽划分統計個數
hue:在x或y標簽划分的同時,再以hue標簽划分統計個數
data:df或array或array列表,用於繪圖的數據集,x或y缺失時,data參數為數據集,同時x或y不可缺少,必須要有其中一個
order, hue_order:分別是對x或y的字段排序,hue的字段排序。排序的方式為列表
orient:強制定向,v:豎直方向;h:水平方向
palette:使用不同的調色板
ax:畫子圖的時候
例子
import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt titanic=pd.read_csv('F://python//titanic_data.csv') titanic.columns #x軸上的條形圖 sns.countplot(x='Pclass',data=titanic) #或者直接使用df[col] sns.countplot(x=titanic['Pclass'])
#y軸上的條形圖 sns.countplot(y='Pclass',data=titanic) #或者直接使用df[col] sns.countplot(y=titanic['Pclass'])
#hue sns.countplot(x='Pclass',hue='Survived',data=titanic) #或者直接使用df[col] sns.countplot(x=titanic['Pclass'],hue=titanic['Survived'])
#order,hue_order sns.countplot(x='Pclass',hue='Survived',data=titanic,order=[3,2,1],hue_order=[1,0])
#調色板 sns.countplot(x='Pclass',data=titanic,palette="Set3")
#ax指定子圖 fig, ax = plt.subplots(1, 2, figsize=(10, 5)) sns.countplot(x='Pclass', data=titanic, ax=ax[0]) sns.countplot(y='Pclass', data=titanic, ax=ax[1])