【數據分析與可視化】直方圖和密度圖

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt

直方圖

# randn正態分佈
s = Series(np.random.randn(1000))
# 直方圖和柱狀圖不同(是一個取值範圍)
plt.hist(s, rwidth=0.9)
(array([  8.,  27.,  76., 190., 242., 225., 147.,  65.,  16.,   4.]),
 array([-3.09294876, -2.46028907, -1.82762939, -1.1949697 , -0.56231002,
         0.07034966,  0.70300935,  1.33566903,  1.96832872,  2.6009884 ,
         3.23364809]),
 <a list of 10 Patch objects>)

在這裏插入圖片描述

# 理解取值範圍分佈直方圖
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 0-2之間有一個,依次如此
plt.hist(a, rwidth=0.9)
(array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
 array([0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9. ]),
 <a list of 10 Patch objects>)

在這裏插入圖片描述

# 理解Series的數據,直方圖tuple數據類型
re = plt.hist(s, rwidth=0.9)
len(re)
3

在這裏插入圖片描述

type(re)
tuple
# 頻率
re[0]
array([  8.,  27.,  76., 190., 242., 225., 147.,  65.,  16.,   4.])
# 間隔
re[1]
array([-3.09294876, -2.46028907, -1.82762939, -1.1949697 , -0.56231002,
        0.07034966,  0.70300935,  1.33566903,  1.96832872,  2.6009884 ,
        3.23364809])
re[2]
<a list of 10 Patch objects>
# 參數修改間隔 默認10 bins=20,顏色,水平
plt.hist(s, rwidth=0.9, bins=20, color='red')
(array([  6.,   2.,   8.,  19.,  30.,  46.,  76., 114., 113., 129., 121.,
        104.,  85.,  62.,  45.,  20.,  11.,   5.,   3.,   1.]),
 array([-3.09294876, -2.77661892, -2.46028907, -2.14395923, -1.82762939,
        -1.51129955, -1.1949697 , -0.87863986, -0.56231002, -0.24598018,
         0.07034966,  0.38667951,  0.70300935,  1.01933919,  1.33566903,
         1.65199888,  1.96832872,  2.28465856,  2.6009884 ,  2.91731824,
         3.23364809]),
 <a list of 20 Patch objects>)

在這裏插入圖片描述

密度圖

s.plot(kind='kde')

在這裏插入圖片描述
<matplotlib.axes._subplots.AxesSubplot at 0x1a260c38d0>

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