数据分析作业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()
相关推荐
田里的水稻9 小时前
DT_digital_twin_ROS+Grazebo仿真
深度学习·数据挖掘·数据分析
kangk1210 小时前
单细胞转录组分析流程十一(细胞通讯,cellchat,双(多)样本)
数据挖掘·单细胞
我爱鸢尾花10 小时前
第十四章聚类方法理论及Python实现
大数据·python·机器学习·数据挖掘·数据分析·聚类
Tiger Z13 小时前
《R for Data Science (2e)》免费中文翻译 (第14章) --- Strings(2)
数据分析·r语言·数据科学·免费书籍
非著名架构师14 小时前
“低空经济”的隐形护航者:AI驱动的秒级风场探测如何保障无人机物流与城市空管安全?
人工智能·数据分析·疾风气象大模型·高精度天气预报数据·galeweather.cn·高精度气象
洁洁!14 小时前
openEuler在WSL2中的GPU加速AI训练实战指南
人工智能·数据挖掘·数据分析
非著名架构师16 小时前
从“人找信息”到“信息找人”:气象服务模型如何主动推送风险,守护全域安全?
大数据·人工智能·安全·数据分析·高精度天气预报数据·galeweather.cn
clarance201517 小时前
ChatBI王者之争:ThoughtSpot、Databricks、Power BI等五大产品深度对决与选型指南
大数据·人工智能·信息可视化·数据挖掘·数据分析
梦仔生信进阶18 小时前
【Linux】生物信息学Linux入门指南:从核心命令到实战应用
数据分析