說點什么呢
seaborn
是python的一個可視化模塊盈简,構建于matplotlib
之上凑耻,對于pandas
的DataFrame
這種數(shù)據(jù)結構有著很好的支持太示,能夠大大減少構建繪圖數(shù)據(jù)是所需的工作量(我也不知道自己為什么要裝逼不去用R的ggplot2
)。
countplot
到底要不要點
有時候我們需要對DataFrame
中的某一列或兩列進行統(tǒng)計(多了我也不會)香浩,省去去構建barplot
所需的數(shù)據(jù)這一過程类缤,seaborn
的countplot
能夠很好的勝任這一……(這一什么?使命~ 真的是詞窮啊~)
(markdown
竟然有下標 可以)
Show the counts of observations in each categorical bin using bars.
使用bars來表示每個分類數(shù)據(jù)的數(shù)目
如果你沒有打開上面的鏈接邻吭,那么~我把它搬過來了呀非。
像所有的matplotlib
函數(shù)那樣,這個函數(shù)也有著挺多的參數(shù)镜盯,但是平時用到的應該也就這幾個
x, y, hue : names of variables in data or vector data, optional
data : DataFrame, array, or list of arrays, optional
order, hue_order : lists of strings, optional
ax
data
平時我用的也就是DataFrame
的名字
ax
用來指定坐標系(不明白的話去補一下matplotlib
的知識)岸裙。
x
比較容易理解,就是因子所在的列名(平時使用的都是DataFrame
)速缆,如果只給了x
這一個指定數(shù)據(jù)的參數(shù)降允,那么就會匯出這個每個因子所包含的記錄的數(shù)目(ipython notebook)
import seaborn as sns
%matplotlib inline
titanic_df=sns.load_dataset("titanic")
titanic_df.head(5) #看一下數(shù)據(jù)什么樣子
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True
# 用y的話就變成水平的了
sns.countplot(x = 'survived',data=titanic_df)
這里是僅僅繪出了titanic_df
的survived
這一列數(shù)據(jù)中兩類的數(shù)目
有500+的遇難者(‘0’),300+的幸存者(‘1’)艺糜。
(ps:這里使用的是seaborn
自帶的泰坦尼克的數(shù)據(jù)剧董,對就是那艘首秀即謝幕的巨輪)
如果我們同時要查看在這些遇難者以及幸存者中,男人以及女人的數(shù)目破停,就需要加入另外一個參數(shù)hue
(我始終不明白為什么要用這個詞~)
# 依舊是上面的數(shù)據(jù)
sns.countplot(x = 'survived',hue = 'sex',data=titanic_df)
遇難者以及幸存者男女比例一目了然翅楼,這很gentleman。
嗯~就到這里
如果想要進行更多的定制話操作真慢,可以參照axis
對象的方法(matplotlib
中)毅臊,由于這個函數(shù)是有返回值的,返回的是一個matplotlib Axes
對象黑界,因此管嬉,可以通過面向對象的方法進行后續(xù)axis
所支持的各種操作。
# 中文無法正常顯示朗鸠,好像字體問題蚯撩,沒有深究。
g = sns.countplot(x = 'survived',hue = 'sex',data=titanic_df)
g.set_title('title')