数据分析作业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()
相关推荐
Michelle80235 小时前
23大数据 数据挖掘复习1
大数据·人工智能·数据挖掘
补三补四5 小时前
GB级csv文件处理
数据分析
梦想画家8 小时前
Cohen‘s Kappa系数:衡量分类一致性的黄金标准及其在NLP中的应用
自然语言处理·分类·数据挖掘
iceslime10 小时前
头歌Educator机器学习与数据挖掘-逻辑回归
机器学习·数据挖掘·逻辑回归
RE-190120 小时前
Excel基础知识 - 导图笔记
数据分析·学习笔记·excel·思维导图·基础知识·函数应用
eqwaak01 天前
数据预处理与可视化流水线:Pandas Profiling + Altair 实战指南
开发语言·python·信息可视化·数据挖掘·数据分析·pandas
Christo31 天前
关于K-means和FCM的凸性问题讨论
人工智能·算法·机器学习·数据挖掘·kmeans
生物小卡拉1 天前
R脚本——Deseq2差异表达分析--v1.0
windows·经验分享·笔记·学习·信息可视化·数据分析
热心不起来的市民小周1 天前
基于 RoBERTa + 多策略优化的中文商品名细粒度分类
人工智能·分类·数据挖掘
韩立学长2 天前
【开题答辩实录分享】以《基于python的奶茶店分布数据分析与可视化》为例进行答辩实录分享
开发语言·python·数据分析