numpy 直方图绘制(参考)

import matplotlib.pyplot as plt
import numpy as np
import math


# 薪水情况
salary = [3500, 4200, 8000, 13000, 13000, 2000, 3000, 4300, 3500, 25000, 30000, 2300, 4000, 6000]

# 统计薪水人数最多的一个区间
# 统计薪水落在各个区间的数目 --确定众数在哪个区间
# 评估这个公司的薪资水平
# 创建画布
plt.figure()

# 准备数据
# 指定分组数目
# bins = 5

# 自定义分组
# 找到最大值、最小值 相减 --极差
max = np.max(salary)
min = np.min(salary)
# 极差
ptp = max-min

# 指定组距 --3000一组
bins = math.ceil(ptp/3000)

# 组距
arr = np.arange(min, (3000*bins)+3001, 3000)

# 绘图
plt.hist(salary, bins=bins, color='g')

# 修改刻度
plt.xticks(arr)

# 图形展示
plt.show()

# 结论 大部分员工薪水在5000以下 -- 老板该涨薪资了

猜你喜欢

转载自blog.csdn.net/YPL_ZML/article/details/93408845
今日推荐