Seaborn barplot柱狀圖/條形圖-基於matplotlib的更強力製圖庫

相信大家在開始python的使用後,便隨後接觸到了matplotlib這個與python兼容的很好的製圖庫。但是,如果想把圖做的更細,更上流,那麼則需要seaborn這個庫,比起matplotlib更容易上手,並且和pandas的兩種主要數據結構Series和DataFrame有着很強的兼容性。

1 . 安裝,工欲善其事,安裝 seaborn,有兩種方法。

pip install seaborn

conda install seaborn

在這裏我用的conda的客戶端版。

 

2. 對於seaborn的介紹,我們從最簡單也是最常用的"柱狀圖"開始。

barplot便是seaborn庫的柱狀圖方法。

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

seaborn所做的柱狀圖

2.1 數據集介紹

在這裏我們使用seaborn自帶的數據集tips。

import seaborn as sns
tips = sns.load_dataset("tips")
print(tips.head())

>>>
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

Tips是Dataframe結構, 其矩陣爲(244, 7),一共有244條數據,7個屬性,分別爲

  • total_bill: 賬單總額
  • tip: 小費
  • sex: 性別
  • smoker: 是否抽菸
  • day: 周幾(週一 至 週日)
  • time: 上午下午
  • size: 人數

2.2 seaborn.barplot 方法使用以及參數介紹

2.2.1: 嘗試 barplot

sns.barplot(x = 'day', y = 'total_bill', data = tips)
plt.show()
# 黑線表示置信區間(Confidence interval)

圖2.2.1

橫座標:周幾

縱座標:賬單金額

上圖黑線爲縱座標數據的統計數據的

置信區間_百度百科​baike.baidu.com圖標

,如果不改動的話默認爲平均數。

 

2.2.2 使用 hue 分類每一列數據,按性別來分

還是剛纔的數據,將賬單金額按男女性別劃分開來,使用到參數 hue 。

sns.barplot(x = 'day', y = 'total_bill', hue= 'sex',data= tips)
plt.show()

圖2.2.2

通過 hue 的設置,可以將圖2.2.1按性別更加細化。

2.2.3 畫一個horizontal (水平方向)的圖

sns.barplot(x = 'tip', y = 'day', data=tips)
plt.show()

圖2.2.3.1

在barplot 有一個參數爲orient, 參數包括 ’v' 和‘h’ ,分別對應垂直方向和水平方向,也可以通過這個函數將圖轉化爲水平或者豎直方向。

sns.barplot(x = 'day', y = 'tip', data = tips, orient='v')
plt.show()

圖2.2.3.2

2.2.4 顯性的控制bar的排列, 每一列的排序是默認的,但是可以通過參數 order 進行修改

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.set_title("default")
ax2.set_title("after changing order")

sns.barplot(x='time', y = 'tip', data=tips, ax = ax1)
# lunch, dinner
sns.barplot(x='time', y = 'tip', data=tips, order=['Dinner', 'Lunch'], ax = ax2)
# become Dinner, lunch
plt.show()

2.2.5 通過修改 estimator 調整統計方式

estimator 可以引用 numpy中的方法,例如max, min, median等。更多的方法可以參考

Statistics - NumPy v1.17 Manual​docs.scipy.org

 

from numpy import median, max, min
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
ax1.set_title('default average')
ax2.set_title('max')
ax3.set_title('median')
ax4.set_title('min')
sns.barplot(x="day", y="tip", data=tips, ax = ax1)
sns.barplot(x="day", y="tip", data=tips, estimator= max, ax = ax2)
sns.barplot(x="day", y="tip", data=tips, estimator= median, ax = ax3)
sns.barplot(x="day", y="tip", data=tips, estimator= min, ax = ax4)
plt.show()

2.2.6 通過修改 ci 調整置信區間的大小,默認爲95%

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.set_title("default")
ax2.set_title("after changing ")

sns.barplot(x="day", y="tip", data=tips,ax = ax1)
sns.barplot(x="day", y="tip", data=tips, ci=45, ax = ax2)
plt.show()

ci爲45後,黑線明顯變短

2.2.7 ci 還有一個特殊的參數 ci = sd, 則直接使用標準差(standard deviation)進行統計

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.set_title("default")
ax2.set_title("ci = sd ")
sns.barplot(x="day", y="tip", data=tips,ax = ax1)
sns.barplot(x="day", y="tip", data=tips, ci='sd', ax = ax2)
plt.show()

2.2.8 通過 capsize 調整 ”帽子寬度“, 默認是 None

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
sns.barplot(x="day", y="tip", data=tips, capsize= 0.2, ax = ax1)
sns.barplot(x="day", y="tip", data=tips, capsize= 0.4, ax = ax2)
plt.show()

2.2.9 調整 palette 換一種其他的顏色調色板

可用的參數可以看

seaborn.color_palette - seaborn 0.10.1 documentation​seaborn.pydata.org圖標

着這裏只舉幾個常用的例子

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
ax1.set_title("deep")
ax2.set_title('muted')
ax3.set_title('bright')
ax4.set_title('Blues_d')
sns.barplot("size", y="total_bill", data=tips, palette="deep", ax = ax1)
sns.barplot("size", y="total_bill", data=tips, palette="muted", ax = ax2)
sns.barplot("size", y="total_bill", data=tips, palette="bright", ax = ax3)
sns.barplot("size", y="total_bill", data=tips, palette="Blues_d", ax = ax4)
plt.show()

 

2.2.9 hue 的進一步使用

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
tips['weekend'] = tips['day'].isin(['Sat','Sun'])
sns.barplot(x='day', y='tip', data=tips, hue='weekend', ax = ax1)
sns.barplot(x='day', y='tip', data=tips, hue='weekend', dodge=False, ax = ax2)
plt.show()

默認的hue使用後造成不美觀的現象,使用 dodge=False 可以將空白補齊。

2.2.10 顏色的精細化調整, color 和 saturation(飽和度)的使用

ax = sns.barplot("size", y="total_bill", data=tips,
                 color="salmon", saturation=.5)
plt.show()

 

 

總結,我們將barplot基本的常用參數都演示了一遍,當然seaborn還有直方圖和散點圖等工具。

 

可以關注我的公衆號 : 平凡的科研生活源代碼將附在原文中同時也會有其他的數據分析相關的文章分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章