【数据分析与可视化】直方图和密度图

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>

原创文章 257 获赞 187 访问量 16万+

猜你喜欢

转载自blog.csdn.net/weixin_43469680/article/details/105716327
今日推荐