数据分析作业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()
相关推荐
2501_921649491 小时前
免费获取股票历史行情与分时K线数据 API
开发语言·后端·python·金融·数据分析
Hcoco_me1 小时前
RTMPose_JSON相关解读
算法·数据挖掘·json·聚类
职业码农NO.14 小时前
智能体推理范式: Plan-and-Execute(规划与执行)
人工智能·python·数据分析·系统架构·知识图谱·agent·集成学习
STLearner5 小时前
VLDB 2025 | 时间序列(Time Series)论文总结(预测,异常检测,压缩,自动化等)
数据库·人工智能·深度学习·神经网络·机器学习·数据挖掘·时序数据库
2501_941418556 小时前
腰果病害图像识别 Mask-RCNN HRNetV2P实现 炭疽病 锈病 健康叶片分类
人工智能·分类·数据挖掘
墨_浅-11 小时前
教育/培训行业智能体应用分类及知识库检索模型微调
人工智能·分类·数据挖掘
咕噜企业分发小米12 小时前
阿里云基因测序数据分析平台有哪些成功案例?
阿里云·数据分析·云计算
CryptoPP14 小时前
印度股票市场数据获取与分析实战:基于RESTful API与Python
数据挖掘·数据分析
adaAS141431514 小时前
【矿物识别】基于改进YOLO13-C3k2-ContextGuided的铝土矿智能检测与分类系统
人工智能·分类·数据挖掘
HyperAI超神经14 小时前
预测精度可提升60%,清华李勇团队提出神经符号回归方法,自动推导高精度网络动力学公式
人工智能·ai·数据挖掘·地球科学·神经符号