Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a311b7f

Browse files
committed
上传发文代码
1 parent 580b112 commit a311b7f

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

‎populationone/anaone.py‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import numpy as np
7+
import pandas as pd
8+
import pyecharts.options as opts
9+
from pyecharts.charts import Line, Bar, Page, Pie
10+
11+
12+
# 读取数据
13+
pdata = pd.read_excel('populationone.xlsx')
14+
15+
16+
# 分析总人口
17+
def analysis_total():
18+
# 处理数据
19+
x_data = pdata['年份'].tolist()
20+
# 将人口单位转换为亿
21+
y_data1 = pdata['年末总人口(万人)'].map(lambda x: "%.2f" % (x / 10000)).tolist()
22+
y_data2 = pdata['人口自然增长率(‰)'].tolist()
23+
y_data3 = pdata['人口出生率(‰)'].tolist()
24+
y_data4 = pdata['人口死亡率(‰)'].tolist()
25+
26+
# 总人口柱状图
27+
bar = Bar(init_opts=opts.InitOpts(width="1200px", height="500px"))
28+
bar.add_xaxis(x_data)
29+
bar.add_yaxis("年末总人口(亿)", y_data1, category_gap="10%", label_opts=opts.LabelOpts(rotate=90, position="inside"))
30+
bar.set_global_opts(
31+
title_opts=opts.TitleOpts(title="年末总人口变化情况", pos_bottom="bottom", pos_left="center"),
32+
xaxis_opts=opts.AxisOpts(
33+
type_="category",
34+
name='年份',
35+
# 坐标轴名称显示位置
36+
name_location='end',
37+
# x轴数值与坐标点的偏移量
38+
# boundary_gap=False,
39+
axislabel_opts=opts.LabelOpts(is_show=True, margin=10, color="#000", interval=1, rotate=90),
40+
# axisline_opts=opts.AxisLineOpts(is_show=True, symbol="arrow"),
41+
axistick_opts=opts.AxisTickOpts(is_show=True, is_align_with_label=True),
42+
axispointer_opts=opts.AxisPointerOpts(type_="line", label=opts.LabelOpts(is_show=True))
43+
),
44+
# y轴相关选项设置
45+
yaxis_opts=opts.AxisOpts(
46+
type_="value",
47+
position="left",
48+
),
49+
legend_opts=opts.LegendOpts(is_show=True)
50+
)
51+
52+
# bar.render('bartest.html')
53+
54+
# 自然增长率、出生率、死亡率折线图
55+
line = Line(init_opts=opts.InitOpts(width="1400px", height="500px"))
56+
line.add_xaxis(x_data)
57+
line.add_yaxis(
58+
series_name="自然增长率(‰)",
59+
y_axis=y_data2,
60+
label_opts=opts.LabelOpts(
61+
is_show=False
62+
)
63+
)
64+
line.add_yaxis('出生率(‰)', y_data3, label_opts=opts.LabelOpts(is_show=False))
65+
line.add_yaxis('死亡率(‰)', y_data4, label_opts=opts.LabelOpts(is_show=False))
66+
line.set_global_opts(
67+
title_opts=opts.TitleOpts(title="人口自然增长率、出生率、死亡率", pos_bottom="bottom", pos_left="center"),
68+
xaxis_opts=opts.AxisOpts(
69+
name='年份',
70+
name_location='end',
71+
type_="value",
72+
min_="1949",
73+
max_interval=1,
74+
# 设置x轴不必与y轴的0对齐
75+
axisline_opts=opts.AxisLineOpts(is_on_zero=False),
76+
axislabel_opts=opts.LabelOpts(is_show=True, color="#000", interval=0, rotate=90),
77+
axistick_opts=opts.AxisTickOpts(is_show=True, is_align_with_label=True),
78+
axispointer_opts=opts.AxisPointerOpts(type_="shadow", label=opts.LabelOpts(is_show=True))
79+
),
80+
# y轴相关选项设置
81+
yaxis_opts=opts.AxisOpts(
82+
name='比例',
83+
type_="value",
84+
position="left",
85+
min_=-10,
86+
axislabel_opts=opts.LabelOpts(is_show=True)
87+
),
88+
legend_opts=opts.LegendOpts(is_show=True)
89+
)
90+
91+
# 渲染图像,将多个图像显示在一个html中
92+
# DraggablePageLayout表示可拖拽
93+
page = Page(layout=Page.DraggablePageLayout)
94+
page.add(bar)
95+
page.add(line)
96+
page.render('population_total.html')
97+
98+
# 分析男女比
99+
def analysis_sex():
100+
x_data = pdata['年份'].tolist()
101+
# 历年男性人口数
102+
y_data_man = pdata['男性人口(万人)']
103+
# 历年女性人口数
104+
y_data_woman = pdata['女性人口(万人)']
105+
# 2019年男女比饼图
106+
sex_2019 = pdata[pdata['年份'] == 2019][['男性人口(万人)', '女性人口(万人)']]
107+
108+
# 两列相减,获得新列
109+
y_data_man_woman = pdata['男性人口(万人)'] - pdata['女性人口(万人)']
110+
111+
pie = Pie()
112+
pie.add("", [list(z) for z in zip(['男', '女'], np.ravel(sex_2019.values))])
113+
pie.set_global_opts(title_opts=opts.TitleOpts(title="2019中国男女比", pos_bottom="bottom", pos_left="center"))
114+
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
115+
pie.render('nvpie.html')
116+
117+
line = Line(init_opts=opts.InitOpts(width="1400px", height="500px"))
118+
line.add_xaxis(x_data)
119+
line.add_yaxis(
120+
series_name="男女差值",
121+
y_axis=y_data_man_woman.values,
122+
# 标出关键点的数据
123+
markpoint_opts=opts.MarkPointOpts(
124+
data=[
125+
opts.MarkPointItem(type_="min"),
126+
opts.MarkPointItem(type_="max"),
127+
opts.MarkPointItem(type_="average")
128+
]
129+
),
130+
label_opts=opts.LabelOpts(
131+
is_show=False
132+
),
133+
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")])
134+
)
135+
line.set_global_opts(
136+
title_opts=opts.TitleOpts(title="中国70年(1949-2019)男女差值(万人)", pos_left="center", pos_top="bottom"),
137+
legend_opts=opts.LegendOpts(is_show=False),
138+
xaxis_opts=opts.AxisOpts(
139+
name='年份',
140+
name_location='end',
141+
type_="value",
142+
min_="1949",
143+
max_interval=1,
144+
# 设置x轴不必与y轴的0对齐
145+
axisline_opts=opts.AxisLineOpts(is_on_zero=False),
146+
axislabel_opts=opts.LabelOpts(is_show=True, color="#000", interval=0, rotate=90),
147+
axistick_opts=opts.AxisTickOpts(is_show=True, is_align_with_label=True),
148+
axispointer_opts=opts.AxisPointerOpts(type_="shadow", label=opts.LabelOpts(is_show=True))
149+
),
150+
yaxis_opts=opts.AxisOpts(
151+
name='差值(万人)',
152+
type_="value",
153+
position="left",
154+
axislabel_opts=opts.LabelOpts(is_show=True)
155+
),
156+
)
157+
158+
# 5、渲染图像,将多个图像显示在一个html中
159+
page = Page(layout=Page.DraggablePageLayout)
160+
page.add(pie)
161+
page.add(line)
162+
page.render('population_sex.html')
163+
164+
165+
if __name__ == '__main__':
166+
analysis_total()
167+
analysis_sex()

‎populationone/populationone.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import pandas as pd
7+
import requests
8+
9+
# 人口数量excel文件保存路径
10+
POPULATION_EXCEL_PATH = 'populationone.xlsx'
11+
12+
# 爬取人口数据
13+
def spider_population():
14+
# 请求参数 sj(时间),zb(指标)
15+
# 总人口
16+
dfwds1 = '[{"wdcode": "sj", "valuecode": "LAST70"}, {"wdcode":"zb","valuecode":"A0301"}]'
17+
# 人口出生率、死亡率、自然增长率
18+
dfwds2 = '[{"wdcode": "sj", "valuecode": "LAST70"}, {"wdcode":"zb","valuecode":"A0302"}]'
19+
url = 'http://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=hgnd&rowcode=sj&colcode=zb&wds=[]&dfwds={}'
20+
# 将所有数据放这里,年份为key,值为各个指标值组成的list
21+
# 因为 2019 年数据还没有列入到年度数据表里,所以根据统计局2019年经济报告中给出的人口数据计算得出
22+
# 数据顺序为历年数据
23+
population_dict = {
24+
25+
}
26+
27+
response1 = requests.get(url.format(dfwds1))
28+
get_population_info(population_dict, response1.json())
29+
30+
response2 = requests.get(url.format(dfwds2))
31+
get_population_info(population_dict, response2.json())
32+
33+
population_dict['2019'] = [2019, 140005, 71527, 68478, 84843, 55162, 10.48, 7.14, 3.34]
34+
save_excel(population_dict)
35+
36+
return population_dict
37+
38+
# 提取人口数量信息
39+
def get_population_info(population_dict, json_obj):
40+
datanodes = json_obj['returndata']['datanodes']
41+
for node in datanodes:
42+
# 获取年份
43+
year = node['code'][-4:]
44+
# 数据数值
45+
data = node['data']['data']
46+
if year in population_dict.keys():
47+
population_dict[year].append(data)
48+
else:
49+
population_dict[year] = [int(year), data]
50+
return population_dict
51+
52+
# 人口数据生成excel文件
53+
def save_excel(population_dict):
54+
# .T 是行列转换
55+
df = pd.DataFrame(population_dict).T[::-1]
56+
df.columns = ['年份', '年末总人口(万人)', '男性人口(万人)', '女性人口(万人)', '城镇人口(万人)', '乡村人口(万人)', '人口出生率(‰)', '人口死亡率(‰)',
57+
'人口自然增长率(‰)']
58+
writer = pd.ExcelWriter(POPULATION_EXCEL_PATH)
59+
# columns参数用于指定生成的excel中列的顺序
60+
df.to_excel(excel_writer=writer, index=False, encoding='utf-8', sheet_name='中国70年人口数据')
61+
writer.save()
62+
writer.close()
63+
64+
65+
if __name__ == '__main__':
66+
result_dict = spider_population()
67+
# print(result_dict)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /