-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathNCP_historical_data_visualization.py
339 lines (304 loc) · 11.1 KB
/
NCP_historical_data_visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import pandas as pd
import datetime
from typing import List
import pyecharts.options as opts
from pyecharts.globals import ThemeType
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Timeline, Grid, Bar, Map, Pie, Line
# 显示所有列(参数设置为None代表显示所有行,也可以自行设置数字)
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
# 设置数据的显示长度,默认为50
pd.set_option('max_colwidth', 200)
# 禁止自动换行(设置为False不自动换行,True反之)
pd.set_option('expand_frame_repr', False)
url = 'https://raw.githubusercontent.com/BlankerL/DXY-2019-nCoV-Data/master/csv/DXYArea.csv'
df = pd.read_table(url, sep=',')
provinceName = ["上海", "云南", "内蒙古", "北京", "吉林", "四川",
"天津", "宁夏", "安徽", "山东", "山西", "广东",
"广西", "新疆", "江苏", "江西", "河北", "河南",
"浙江", "海南", "湖北", "湖南", "甘肃", "福建",
"西藏", "贵州", "辽宁", "重庆", "陕西", "青海", "黑龙江"]
# 格式化DataFrame的日期
df.drop(['cityName', 'city_confirmedCount', 'city_suspectedCount',
'city_curedCount', 'city_deadCount'], inplace=True, axis=1)
df['updateTime'] = pd.to_datetime(df['updateTime'])
df['updateTime'] = df['updateTime'].apply(
lambda x: datetime.datetime.strftime(x, '%m-%d'))
print(df)
df['provinceName'] = df['provinceName'].apply(
lambda y: y[:3] if y == '内蒙古自治区' or y == '黑龙江省' else y[:2])
# 数据去重(一日内更新了多次的)
NCP_data = df.drop_duplicates(
subset=['provinceName', 'updateTime'], keep='first', inplace=False)
print(df.shape)
print(NCP_data.shape)
# print(NCP_data)
# NCP_data.to_excel(excel_writer="tmp.xlsx",index=False,encoding='utf-8')
# 获取日期list
date_A = []
dateSeries = NCP_data.iloc[:, 9]
dateSeries.drop_duplicates(inplace=True)
date = dateSeries.to_list()
for i in date:
lem_a = date.index(i)
if (lem_a%2) == 0:
date_A.append(i)
date = date_A
confirmed_date = {}
province_percent = {}
total_num = []
MapData = []
dict = {}
data = []
dict2 = {}
# list = []
tmpProvinceList = []
rdate = list(date)
rdate.reverse()
for i in date:
# print(i)
criteria = NCP_data['updateTime'] == i
df = NCP_data[criteria]
for index in df.index:
tmpProvinceList.append(df.loc[index, 'provinceName'])
qlist = list(set(provinceName) - (set(tmpProvinceList)))
# 缺失省份的名单
print(qlist)
old_suspectedCount = 0
old_curedCount = 0
old_deadCount = 0
old_confirmedCount = 0
isChanged = False
for q in qlist:
for td in rdate[:rdate.index(i)]:
criteria = NCP_data['updateTime'] == td
tddf = NCP_data[criteria]
for j in tddf.index:
if str(tddf.loc[j, 'provinceName']) == q:
old_confirmedCount = int(
tddf.loc[j, 'province_confirmedCount'])
old_curedCount = int(tddf.loc[j, 'province_curedCount'])
old_deadCount = int(tddf.loc[j, 'province_deadCount'])
old_suspectedCount = int(
tddf.loc[j, 'province_suspectedCount'])
isChanged = True
break
if isChanged:
NCP_data = NCP_data.append({'provinceName': q,
'province_suspectedCount': old_suspectedCount,
'province_curedCount': old_curedCount,
'province_deadCount': old_deadCount,
'province_confirmedCount': old_confirmedCount,
'updateTime': i
}, ignore_index=True)
isChanged = False
tmpProvinceList.clear()
all_cases = df['province_confirmedCount'].sum()
confirmed_date[i] = all_cases
total_num.append(all_cases / 100)
total_num.sort()
NCP_data.reset_index(drop=True)
isGotGuangDong = False
isGotHuBei = False
maxNum = 0
maxCount = 0
for i in date:
criteria = NCP_data['updateTime'] == i
df = NCP_data[criteria]
df['percent'] = df['province_confirmedCount'] / confirmed_date[i]
print(df.shape)
for index in df.index:
data.append({'name': df.loc[index, 'provinceName'], 'value': [int(
df.loc[index, 'province_confirmedCount']), float(df.loc[index, 'percent']),
str(df.loc[index, 'provinceName'])]})
if not isGotGuangDong and df.loc[index, 'provinceName'] == '广东':
maxNum = int(df.loc[index, 'province_confirmedCount'])
isGotGuangDong = True
if not isGotHuBei and df.loc[index, 'provinceName'] == '湖北':
maxCount = int(df.loc[index, 'province_confirmedCount'])
isGotHuBei = True
data = sorted(data, key=lambda x: -x['value'][1])
MapData.append({'time': i, 'data': list(data)})
data.clear()
# MapData = json.dumps(MapData, ensure_ascii=False)
# print(MapData)
def Reverse(lst):
return [ele for ele in reversed(lst)]
time_list = Reverse(date)
# print(time_list)
fout = open('detail_content.json', 'w', encoding='utf8')
fout.write(str(MapData))
fout.close()
minNum = maxNum / 25
def get_year_chart(year: str):
map_data = [
[[x["name"], x["value"]] for x in d["data"]] for d in MapData if d["time"] == year
][0]
min_data, max_data = (minNum, maxNum)
data_mark: List = []
i = 0
for x in time_list:
if x == year:
data_mark.append(total_num[i])
else:
data_mark.append("")
i = i + 1
map_chart = (
Map()
.add(
series_name="",
data_pair=map_data,
zoom=1,
center=[119.5, 34.5],
is_map_symbol_show=False,
itemstyle_opts={
"normal": {"areaColor": "#323c48", "borderColor": "#404a59"},
"emphasis": {
"label": {"show": Timeline},
"areaColor": "rgba(255,255,255, 0.5)",
},
},
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="" +
str(year) + "全国各省份NCP实时动态(数据来源:丁香园; 数据仓库:BlankerL/DXY-2019-nCoV-Data)",
subtitle="",
pos_left="center",
pos_top="top",
title_textstyle_opts=opts.TextStyleOpts(
font_size=25, color="rgba(255,255,255, 0.9)"
),
),
tooltip_opts=opts.TooltipOpts(
is_show=True,
formatter=JsCode(
"""function(params) {
if ('value' in params.data) {
return params.data.value[2] + ': ' + params.data.value[0];
}
}"""
),
),
visualmap_opts=opts.VisualMapOpts(
is_calculable=True,
dimension=0,
pos_left="30",
pos_top="center",
range_text=["High", "Low"],
range_color=["lightskyblue", "yellow", "orangered"],
textstyle_opts=opts.TextStyleOpts(color="#ddd"),
min_=min_data,
max_=max_data,
),
)
)
line_chart = (
Line()
.add_xaxis(time_list)
.add_yaxis("", total_num)
.add_yaxis(
"",
data_mark,
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(type_="max")]),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(
title="全国各省份NCP实时动态(单位: 百人)", pos_left="72%", pos_top="5%"
)
)
)
bar_x_data = [x[0] for x in map_data]
bar_y_data = [{"name": x[0], "value": x[1][0]} for x in map_data]
bar = (
Bar()
.add_xaxis(xaxis_data=bar_x_data)
.add_yaxis(
series_name="",
yaxis_data=bar_y_data,
label_opts=opts.LabelOpts(
is_show=True, position="right", formatter="{b} : {c}"
),
)
.reversal_axis()
.set_global_opts(
xaxis_opts=opts.AxisOpts(
max_=maxCount, axislabel_opts=opts.LabelOpts(is_show=False)
),
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(is_show=False)),
tooltip_opts=opts.TooltipOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
is_calculable=True,
dimension=0,
pos_left="10",
pos_top="top",
range_text=["High", "Low"],
range_color=["lightskyblue", "yellow", "orangered"],
textstyle_opts=opts.TextStyleOpts(color="#ddd"),
min_=min_data,
max_=max_data,
),
)
)
pie_data = [[x[0], x[1][0]] for x in map_data]
pie = (
Pie()
.add(
series_name="",
data_pair=pie_data,
radius=["15%", "35%"],
center=["80%", "82%"],
itemstyle_opts=opts.ItemStyleOpts(
border_width=1, border_color="rgba(0,0,0,0.5)"
),
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{b} {d}%"),
legend_opts=opts.LegendOpts(is_show=False),
)
)
grid_chart = (
Grid()
.add(
bar,
grid_opts=opts.GridOpts(
pos_left="10", pos_right="45%", pos_top="50%", pos_bottom="5"
),
)
.add(
line_chart,
grid_opts=opts.GridOpts(
pos_left="65%", pos_right="80", pos_top="10%", pos_bottom="50%"
),
)
.add(pie, grid_opts=opts.GridOpts(pos_left="45%", pos_top="60%"))
.add(map_chart, grid_opts=opts.GridOpts())
)
return grid_chart
if __name__ == '__main__':
timeline = Timeline(
init_opts=opts.InitOpts(
width="1600px", height="900px", theme=ThemeType.DARK)
)
for y in time_list:
g = get_year_chart(year=y)
timeline.add(g, time_point=str(y))
timeline.add_schema(
orient="vertical",
is_auto_play=True,
is_inverse=True,
play_interval=1200,
pos_left="null",
pos_right="5",
pos_top="20",
pos_bottom="20",
width="60",
label_opts=opts.LabelOpts(is_show=True, color="#fff"),
)
timeline.render("NCP.html")
print(MapData)