layout: posttitle: Python3.7统计薪水数据并生成直方图
subtitle: Python3.7统计薪水数据并生成直方图
date: 2019-03-21
author: he xiaodong
header-img: img/default-post-bg.jpg
catalog: true
tags:
- Python
- 分析智联招聘薪资数据
- Python生成直方图
承接上篇文章抓取的数据,然后进行分析岗位数据中最关键的薪资数据,生成直方图,直观展示比例,具体采集并保存数据,请参考上篇文章
待处理的数据如下图示:

步骤:
1.使用 pymysql 查询到 salary 字段数据
2.将数据通过-分裂,然后处理成对应的整型结果,装进 list
3.展示 list 的统计数据并保存图片
代码如下:
# -*- coding:utf-8 -*-import pymysqlimport stringimport matplotlib.pyplot as pltconn = pymysql.connect(host="127.0.0.1",user="root",password="123456",db="business",port=3306,charset="utf8")cur = conn.cursor()cur.execute("select salary from jobs where salary != '薪资面议'")salary = cur.fetchall()data = []for s in range(len(salary)):str = salary[s][0].split('-') # 炸开字符串data.append(int(str[0][:-1]) * 1000) # 截取第一位到倒数第一位,并转成整型 * 1000 为具体薪资金额data.append(int(str[1][:-1]) * 1000)conn.close()cur.close()# print(data)plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签plt.cla() # 重新计算循环中绘制图的花会很有用plt.hist(data, bins=10)plt.xlabel("薪资分布") # 设置X轴的文字plt.ylabel("岗位数量") # 设置Y轴的文字plt.title("薪资统计")plt.savefig(job + "薪资统计.png")
最终效果:

©原创文章
