#导入所需的库import requestsimport jsonimport matplotlib.pyplot as pltimport numpy as npimport tkinter as tkdef fin(name): global response, data, china_data, i, j hd = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0’} response = requests.get(‘http//view.inews.qq.com/g2/getOnsInfo?name=disease_h5’, timeout=10, headers=hd).json() data = json.loads(response[‘data’]) china_data = data[‘areaTree’][0][‘children’] china_set = []#设置一个列表 for i in china_data: i.pop(‘today’)#移出列表 i.pop(‘total’) for j in i[‘children’]: j.pop(‘today’) j[‘total’].pop(‘confirm’) j[‘total’].pop(‘deadRate’) j[‘total’].pop(‘healRate’) for i in china_data:#遍历中国疫情数据 for j in i[‘children’]:#遍历个城市数据 if name == j[‘name’]: return j[‘total’] return Falsedef inputcity(urb1): city = urb1 data=fin(city) return city,data#比较两个城市的疫情数据def compare(urb1,urb2): city1, data1 = inputcity(urb1) city2, data2 = inputcity(urb2) # 这两行代码解决 plt 中文显示的问题 plt.rcParams[‘font.sans-serif’] = [‘SimHei’] plt.rcParams[‘axes.unicode_minus’] = False # 输入统计数据 xlable = (‘确诊人数’, ‘死亡人数’, ‘治愈人数’) allPeople1 = list(data1.values()) allPeople2 = list(data2.values()) bar_width = 0.5 # 设置条形宽度 index_urb1 = np.arange(len(xlable)) # 设置第一个城市条形图的横坐标 index_urb1 = index_urb1 + bar_width # 设置第一个城市条形图的横坐标 #bar函数画出条形图 plt.bar(index_urb1, height=allPeople1, width=bar_width, color=‘r’, label=urb1) plt.bar(index_urb2, height=allPeople2, width=bar_width, color=‘b’, label=urb2) plt.legend() # 显示统计图 plt.xticks(index_urb1 + bar_width / 2, xlable) # 设置横坐标轴刻度位置 plt.ylabel(‘人数’) # 纵坐标轴标题 plt.title(’’) # 图形标题 plt.show()window = tk.Tk() #设置初始窗口大小window.title(" ")#窗口标题设置window.geometry(“300x300+800+800”)#窗口参数设置t1.pack()t2 = tk.Entry(window,show = None)t2.pack()def insert_point(): urb1 = t1.get() urb2 = t2.get() compare(urb1,urb2)b1 =tk.Button(window,text=“point”,width = 10,height = 3,command = insert_point)#图形参数设置b1.pack()window.mainloop()