数据分析作业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()
相关推荐
搞大屏的小北 BI2 小时前
国内旅游:现状与未来趋势分析
信息可视化·数据分析·旅游·数据可视化·bi 工具
Hello.Reader3 小时前
TopK算法在大数据重复数据分析中的应用与挑战
大数据·算法·数据分析
安静的_显眼包O_o3 小时前
【数据分析】DataFrame.query()
数据挖掘·数据分析·pandas
技术无疆5 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
羊小猪~~5 小时前
机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
人工智能·python·机器学习·数据挖掘·数据分析·回归·时序数据库
凭栏落花侧10 小时前
决策树:简单易懂的预测模型
人工智能·算法·决策树·机器学习·信息可视化·数据挖掘·数据分析
wei_shuo11 小时前
偏标记学习+图像分类(论文复现)
学习·分类·数据挖掘
bin915312 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
大神薯条老师18 小时前
Python从入门到高手4.3节-掌握跳转控制语句
后端·爬虫·python·深度学习·机器学习·数据分析
LHNC1 天前
2024.9.29 问卷数据分析
数据分析