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
| import numpy as np import json from pyecharts.charts import Line from pyecharts import options as opts
with open("data/history.json", 'r', encoding='utf-8') as f: json_dict = json.loads(f.read())
history = {} for province in json_dict: history[province] = [] for day in json_dict[province]["data"]: if 20200201 <= day["dateId"] <= 20200407: history[province].append(day["confirmedIncr"])
dates = ["%s-%s" % (str(d["dateId"])[4:6], str(d["dateId"])[6:8]) for d in json_dict["湖北"]["data"] if 20200201<=d["dateId"]<=20200407 ]
cn_history = np.array([0] * len(dates)) for province in history: cn_history += np.array(history[province]) cn_history = cn_history.tolist()
hb_history = history["湖北"] ot_history = [cn_history[i]-hb_history[i] for i in range(len(dates))]
line = Line( init_opts=opts.InitOpts(width='1500px', height='700px') ) line.add_xaxis(dates) line.add_yaxis( "全国新增确诊病例", cn_history, is_smooth=True, linestyle_opts=opts.LineStyleOpts(width=3, color="#B44038"), itemstyle_opts=opts.ItemStyleOpts( color='#B44038', border_color="#B44038", border_width=6 ) ) line.add_yaxis( "湖北新增确诊病例", hb_history, is_smooth=True, linestyle_opts=opts.LineStyleOpts(width=2, color="#4E87ED"), label_opts=opts.LabelOpts(position="bottom"), itemstyle_opts=opts.ItemStyleOpts( color="#4E87ED", border_color="#4E87ED", border_width=4 ) ) line.add_yaxis( "其他省份新增确诊", ot_history, is_smooth=True, linestyle_opts=opts.LineStyleOpts(width=2, color="#F1A846"), label_opts=opts.LabelOpts(position="bottom"), itemstyle_opts=opts.ItemStyleOpts( color="#F1A846", border_color="#F1A846", border_width=4 ) ) line.set_global_opts( title_opts=opts.TitleOpts(title="新增确诊趋势图", subtitle="数据来源:丁香园"), yaxis_opts=opts.AxisOpts( max_=16000, min_=1, type_="log", splitline_opts=opts.SplitLineOpts(is_show=True), axisline_opts=opts.AxisLineOpts(is_show=True) ) ) line.render("data/国内新增确诊数趋势图.html")
|