ArcGIS Pro 疫情饼状图Python代码实现
# -*- coding: utf-8 -*- import arcpy import matplotlib.pyplot as plt #解决中文显示问题 plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 fc=arcpy.GetParameterAsText(0)#r"C:\Users\dell\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\a" Fieldname="name" ValueField=arcpy.GetParameterAsText(1) #"SUM_新增确" labels = [] share = [] # 设置分裂属性 explode = [] # 生成数据 with arcpy.da.SearchCursor(fc, (Fieldname, ValueField)) as cursor: for row in cursor: print("Feature {0} = {1}".format(row[0], row[1])) if (row[1]>0): labels.append(row[0]) share.append(row[1]) #explode.append(0) if row[0]=="湖北": explode.append(0.3) else: explode.append(0) plt.figure(figsize=(10,10)) # 分裂饼图 plt.pie(share, explode = explode, labels = labels, autopct='%0f%%', startangle = 180, shadow = True) # 标题 plt.title('2月12日'+ValueField+'-疫情') plt.show()
========================================
import json, requests import csv import time import string import arcpy def updatedata(Shenname,DQname,d1,d2,d3,d4,d5,d6): where_SQL="NL_NAME_1 like '%"+Shenname+"%' and name like '%"+DQname+"%'" #print(where_SQL) n=0 with arcpy.da.UpdateCursor(fc, ("NL_NAME_1", "name", "总确诊", "新增确", "总死亡", "新增死", "总治愈", "新增治"),where_clause=where_SQL) as cursor: for row in cursor: n=n+1 row[2] = d1 row[3] = d2 row[4] = d3 row[5] = d4 row[6] = d5 row[7] = d6 print(Shenname, DQname, d1, d2, d3, d4, d5, d6) cursor.updateRow(row) if (n<1): print("错误=============省=%s,市=%s"%(Shenname,DQname)) #获得某个地区数据 def getData(): #总确诊, 新增确认, 总死亡, 新增死亡, 总治愈, 新增治愈 #当前日期时间戳 number = format(time.time() * 100, '.0f') url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%s' % number #print(url) datas = json.loads(requests.get(url=url).json()['data']) for contry in datas['areaTree']: if contry['name'] == '中国': for province in contry['children']: #print(province['name']) Shenname=province['name'] for city in province['children']: #print("========"+city['name']) DQname=city['name'] d1=city['total']['confirm'] #总确诊 d2=city['today']['confirm'] #新增确 d3=city['total']['dead'] #总死亡 d4=city['today']['dead'] #新增死 d5=city['total']['heal'] #新增死 d6=city['today']['heal'] #新增治 updatedata(Shenname,DQname,d1,d2,d3,d4,d5,d6) #d1,d2,d3,d4,d5,d6=getData("湖北","十堰") #print(d1,d2,d3,d4,d5,d6) fc=r"C:\Users\dell\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\z" getData()
加入省=================
import json, requests import csv import time import string import arcpy def updatedata(Shenname,DQname,d1,d2,d3,d4,d5,d6): where_SQL="NL_NAME_1 like '%"+Shenname+"%' and name like '%"+DQname+"%'" #print(where_SQL) n=0 with arcpy.da.UpdateCursor(fc, ("NL_NAME_1", "name", "总确诊", "新增确", "总死亡", "新增死", "总治愈", "新增治"),where_clause=where_SQL) as cursor: for row in cursor: n=n+1 row[2] = d1 row[3] = d2 row[4] = d3 row[5] = d4 row[6] = d5 row[7] = d6 print(Shenname, DQname, d1, d2, d3, d4, d5, d6) cursor.updateRow(row) if (n<1): print("错误=============省=%s,市=%s"%(Shenname,DQname)) def updatedatasheng(Shenname,d1,d2,d3,d4,d5,d6): where_SQL="NAME like '%"+Shenname+"%'" #print(where_SQL) n=0 with arcpy.da.UpdateCursor(shengfc, ("NAME", "总确诊", "新增确", "总死亡", "新增死", "总治愈", "新增治"),where_clause=where_SQL) as cursor: for row in cursor: n=n+1 row[1] = d1 row[2] = d2 row[3] = d3 row[4] = d4 row[5] = d5 row[6] = d6 print(Shenname, d1, d2, d3, d4, d5, d6) cursor.updateRow(row) if (n!=1): print("错误====================省=%s"%(Shenname)) #获得某个地区数据 def getData(): #总确诊, 新增确认, 总死亡, 新增死亡, 总治愈, 新增治愈 #当前日期时间戳 number = format(time.time() * 100, '.0f') url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%s' % number #print(url) datas = json.loads(requests.get(url=url).json()['data']) for contry in datas['areaTree']: if contry['name'] == '中国': for province in contry['children']: #print(province['name']) Shenname=province['name'] sumd1=0 sumd2 = 0 sumd3 = 0 sumd4 = 0 sumd5 = 0 sumd6 = 0 for city in province['children']: #print("========"+city['name']) DQname=city['name'] d1=city['total']['confirm'] #总确诊 d2=city['today']['confirm'] #新增确 d3=city['total']['dead'] #总死亡 d4=city['today']['dead'] #新增死 d5=city['total']['heal'] #新增死 d6=city['today']['heal'] #新增治 updatedata(Shenname,DQname,d1,d2,d3,d4,d5,d6) sumd1 = sumd1+d1 sumd2 = sumd2 + d2 sumd3 = sumd3 + d3 sumd4 = sumd4 + d4 sumd5 = sumd5 + d5 sumd6 = sumd6 + d6 updatedatasheng(Shenname, sumd1, sumd2, sumd3, sumd4, sumd5, sumd6) #d1,d2,d3,d4,d5,d6=getData("湖北","十堰") #print(d1,d2,d3,d4,d5,d6) fc=r"C:\Users\dell\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\z" shengfc=r"C:\Users\dell\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\s" getData()
===========================================下面慢
import json, requests import csv import time import string import arcpy #获得某个地区数据 def getData(Shenname,DQname): #总确诊, 新增确认, 总死亡, 新增死亡, 总治愈, 新增治愈 #当前日期时间戳 number = format(time.time() * 100, '.0f') url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%s' % number #print(url) datas = json.loads(requests.get(url=url).json()['data']) for contry in datas['areaTree']: if contry['name'] == '中国': for province in contry['children']: #print(province['name']) if province['name']==Shenname: for city in province['children']: #print("========"+city['name']) if city['name']==DQname: return city['total']['confirm'],city['today']['confirm'],city['total']['dead'],city['today']['dead'],city['total']['heal'],city['today']['heal'] return 0,0,0,0,0,0 #d1,d2,d3,d4,d5,d6=getData("湖北","十堰") #print(d1,d2,d3,d4,d5,d6) fc=r"C:\Users\dell\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\z" with arcpy.da.UpdateCursor(fc, ("NL_NAME_1","name","总确诊","新增确","总死亡","新增死","总治愈","新增治")) as cursor: for row in cursor: Shenname=str(row[0]) #print("Shenname="+Shenname) p=Shenname.find("|") if (p>-1): Shenname=Shenname[p+1:] DQname=row[1] #print("DQname=" + DQname) d1, d2, d3, d4, d5, d6 = getData(Shenname, DQname) row[2]= d1 row[3] = d2 row[4] = d3 row[5] = d4 row[6] = d5 row[7] = d6 print(Shenname,DQname,d1,d2,d3,d4,d5,d6) cursor.updateRow(row)