疫情分析数据可视化(基于百度api ,四个国家,古老的代码,放出来记录一下)
import requests
import json
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import pandas as pd
startURL = r"https://voice.baidu.com/newpneumonia/getv2?target=trend&isCaseIn=0&from=mola-virus&area="
endURL = r"&stage=publish"
dict1 = ["美国", "俄罗斯", "印度", "以色列"]
# 日期
country_date = []
# 确诊人数
sure_count = []
# 治愈人数
heal_count = []
# 新增人数
add_count = []
# 死亡人数
death_count = []
Res = []
heal_Res = pd.DataFrame()
if __name__ == '__main__':
plt.figure(1)
plt.rcParams['savefig.dpi'] = 300
plt.rcParams['figure.dpi'] = 300
plt.rcParams['font.sans-serif'] = ['KaiTi', 'SimHei', 'FangSong']
plt.rcParams['font.size'] = 6
plt.rcParams['axes.unicode_minus'] = False
for i in dict1:
url = startURL + i + endURL
res = json.loads(requests.get(url).text)
country_date = res['data'][0]['trend']['updateDate']
country_date_solve = []
header = 2019
for k in country_date:
if str(k) == "1.1":
header = header + 1
k = str(header) + '.' + k
country_date_solve.append(k)
sure_count = res['data'][0]['trend']['list'][0]['data']
heal_count = res['data'][0]['trend']['list'][1]['data']
add_count = res['data'][0]['trend']['list'][3]['data']
death_count = res['data'][0]['trend']['list'][2]['data']
Res.append([country_date_solve, sure_count, heal_count, add_count, death_count])
for i in range(len(dict1)):
plt.plot(Res[i][0], Res[i][1], label=dict1[i])
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(80))
plt.legend()
plt.title('确诊人数')
plt.show()
for i in range(len(dict1)):
plt.plot(Res[i][0], Res[i][3], label=dict1[i])
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(80))
plt.legend()
plt.title('新增人数')
plt.show()
for i in range(len(dict1)):
plt.plot(Res[i][0], Res[i][4], label=dict1[i])
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(80))
plt.legend()
plt.title('死亡人数')
plt.show()
for i in range(len(dict1)):
plt.plot(Res[i][0], Res[i][2], label=dict1[i])
pd.DataFrame(Res[i][0], Res[i][2]).to_csv(str(dict1[i] + '.csv'))
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(80))
plt.legend()
plt.title('治愈人数')
plt.show()