数据分析作业2

中国在 2020 年开展第七次全国人口普查,截止 2021 年 5 月 11 日普查结果公布,全国人口共1411778724人。单从数据表格看相关数据不够直观,需要进行数据可视化展示,方便查看数据结果。

任务一:链接 MySQL 数据库,从历年人口变化表 (history_count), 按年龄人口统计表 (age_count)中取出数据

bash 复制代码
## 连接数据库
conn=pymysql.connect(host = 'localhost' # 连接名称
,user = 'root' # 用户名
,passwd='123456' # 密码
,port= 3306 # 端口,默认为3306
,db='zjq' # 数据库名称
,charset='utf8' # 字符编码
)

任务二:整理历年人口变化数据,按年龄统计人口数量

bash 复制代码
## 历年人口变化表
select year,total from history_count
## 按年龄统计人口数量
select round(sum(area.total * age.age0_14), 0) as age0_14_population,
	round(sum(area.total * age.age15_59), 0) as age15_59_population,
	round(sum(area.total *(age.age60 - age.age65)), 0) as age60_population,
	round(sum(area.total * age.age65), 0) as age65_population
from
	zjq.area_count area
join zjq.age_count age on
	area.area = age.area;

任务三:使用 Matplotlib 进行数据可视化展示,输出图片

bash 复制代码
import pymysql
import pandas as pd
import matplotlib.pyplot as plt
import pylab as mpl

## 在绘制图表时能够正确显示中文,并且负号能够正常显示
mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']
mpl.rcParams['axes.unicode_minus'] = False


## 连接数据库
conn=pymysql.connect(host = 'localhost'
,user = 'root'
,passwd='123456'
,port= 3306
,db='zjq'
,charset='utf8'
)

#开启一个游标cursor
cur = conn.cursor()

#1.1获取历年人口变化表里的所有数据
sql_history="select year,total from history_count"
#1.2执行sql中的语句
cur.execute(sql_history)
#1.3将获取到的sql数据全部显示出来
history_count=cur.fetchall()
#1.4定义需要用上的空数据数组,然后通过遍历数据库的数据将数据附上去
xname=[]
ynum=[]

for x in history_count:
    xname.append(x[0])
    ynum.append(x[1])

#1.5 创建一个figure(一个窗口)来显示条形图
plt.figure()
plt.bar(xname,ynum)
plt.xlabel('year')
plt.ylabel('total')
for x,y in enumerate(ynum):
    plt.text(x,y,'%s'% y)
#1.6显示图表
people=pd.DataFrame(list(history_count),columns=['year','total'])
x=people['year']
y=people['total']
plt.plot(x,y)
plt.xlabel('年份')
plt.ylabel('人口数量')
plt.title('历年人口变化数据')
plt.show()

#2.1获取按年龄人口统计表里的所有数据
sql_age_count = ("select round(sum(area.total * age.age0_14), 0) as age0_14_population,"
                 "round(sum(area.total * age.age15_59), 0) as age15_59_population,"
                 "round(sum(area.total *(age.age60 - age.age65)), 0) as age60_population,"
                 "round(sum(area.total * age.age65), 0) as age65_population"
                 " from zjq.area_count area join zjq.age_count age on"
                 " area.area = age.area;")
#2.2 执行sql语句
cur.execute(sql_age_count)

#2.3 将获取到的sql数据全部显示出来
result=cur.fetchall()

age_radio=pd.DataFrame(list(result),columns=['age0_14','age15_59','age60_64','age65'])
size=[age_radio['age0_14'][0],age_radio['age15_59'][0],age_radio['age60_64'][0],age_radio['age65'][0]]
plt.pie(size,labels=['age0_14','age15_59','age60_64','age65'],autopct='%.2f%%', shadow=True)
plt.title('按照年龄统计人口占比')
#2.4显示图表
plt.show()

#关闭游标
cur.close
conn.close()
相关推荐
少林码僧1 分钟前
2.29 XGBoost、LightGBM、CatBoost对比:三大梯度提升框架选型指南
人工智能·机器学习·ai·数据挖掘·数据分析·回归
Golang编程笔记1 小时前
电商数据分析的未来发展路径
ai·数据挖掘·数据分析
2501_936146049 小时前
基于YOLO11-C3k2-Faster-CGLU的草莓成熟度检测与分类系统
人工智能·分类·数据挖掘
城数派9 小时前
2019-2025年各区县逐月新房房价数据(Excel/Shp格式)
大数据·数据分析·excel
橙露9 小时前
从零基础到实战:Python 数据分析三剑客(Pandas+NumPy+Matplotlib)核心应用指南
python·数据分析·pandas
YangYang9YangYan13 小时前
2026高职大数据与会计专业学数据分析的技术价值分析
大数据·数据挖掘·数据分析
少林码僧18 小时前
2.31 机器学习神器项目实战:如何在真实项目中应用XGBoost等算法
人工智能·python·算法·机器学习·ai·数据挖掘
FIT2CLOUD飞致云1 天前
操作教程|DataEase企业总-分公司数据填报场景搭建实践
数据分析·开源·数据可视化·dataease·bi
木头程序员1 天前
大模型边缘部署突破:动态推理技术与精度-延迟-能耗帕累托优化
大数据·人工智能·计算机视觉·自然语言处理·智能手机·数据挖掘
DX_水位流量监测1 天前
无人机测流之雷达流速仪监测技术分析
大数据·网络·人工智能·数据分析·自动化·无人机