【數據分析可視化】seaborn實現直方圖和密度圖

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
%matplotlib inline
# 引入
import seaborn as sns
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
# 正態分佈生成1000個數據
s1 = Series(np.random.randn(1000))

普通畫

# 直方圖
plt.hist(s1)
(array([  1.,  34., 105., 253., 330., 198.,  68.,  10.,   0.,   1.]),
 array([-3.48152703, -2.68232526, -1.88312349, -1.08392171, -0.28471994,
         0.51448183,  1.31368361,  2.11288538,  2.91208715,  3.71128892,
         4.5104907 ]),
 <a list of 10 Patch objects>)

在這裏插入圖片描述

# 密度圖 Series 的方法直接畫.plot
s1.plot(kind='kde')
<matplotlib.axes._subplots.AxesSubplot at 0x1a1e906a90>

在這裏插入圖片描述

seaborn畫

# 直方圖,密度圖.distplot()
# 參數 數據,分塊,是否直方圖,是否密度圖,rug分佈情況
sns.distplot(s1, bins=20, hist=True, kde=True, rug=True)
<matplotlib.axes._subplots.AxesSubplot at 0x1a1faf2410>

在這裏插入圖片描述

# 密度圖
# 參數 數據,顏色填充, 顏色
sns.kdeplot(s1, shade=True, color='r')
<matplotlib.axes._subplots.AxesSubplot at 0x1a2258d850>

在這裏插入圖片描述

sns.rugplot(s1)
<matplotlib.axes._subplots.AxesSubplot at 0x1a227783d0>

在這裏插入圖片描述

調用plot的方法(以前sns可以調用現在不行了)

plt.plot(s1)
[<matplotlib.lines.Line2D at 0x1a225d6650>]

在這裏插入圖片描述

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