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 ()
0 commit comments