前言
見過使用matplotlib畫的直方圖之後,再來領略一下seaborn的強大之處。
sns.distplot()
一個方法搞定兩個圖
seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None)
Parameters:
a : Series, 1d-array, or list.傳入數據序列
Observed data. If this is a Series object with a name attribute, the name will be used to label the data axis.
bins : argument for matplotlib hist(), or None, optional
簡單說,就是設置直方圖的柱子個數
Specification of hist bins, or None to use Freedman-Diaconis rule.
hist : bool, optional
是否要畫直方圖(重點)
Whether to plot a (normed) histogram.
kde : bool, optional
是否要畫密度圖(重點)
Whether to plot a gaussian kernel density estimate.
rug : bool, optional
是否要畫底部的那個密度…???,不知道咋說,運行代碼看效果吧
Whether to draw a rugplot on the support axis.
太多了,詳情參考:官方文檔傳送門
demo
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
import seaborn as sns
np.random.seed(666)
s1 = Series(np.random.randn(1000))
# 可以同時畫出直方圖和密度圖
sns.distplot(s1, hist = True, kde = True, rug = True)
plt.show() # 圖1
# 還有一個函數 是 kde
sns.kdeplot(s1, shade = True) # 會將曲線下方的區域進行一個填充
plt.show() # 圖2
通過對 hist = True, kde = True 這兩個參數的控制,就可以畫出密度圖和直方圖。
圖:
圖2:
seaborn.kdeplot() 專業畫密度圖
參數就省略了,感覺使用第一個方法就夠用了,學有餘力的同學請參考:官方文檔傳送門