黑馬程序員python教程,8天python從入門到精通,學(xué)python看這套就

P106 全國疫情地圖
注意所給txt文件中給的省份名稱都是簡寫,會(huì)導(dǎo)致無法生效。如:”北京“要改為“北京市”,”寧夏“要改為”寧夏回族自治區(qū)“,"香港"要改為”香港特別行政區(qū)“等等。
具體給出以下解決代碼:
import json
from pyecharts.charts import Map
from pyecharts.options import *
f = open("C:/Users/prophet/Desktop/new/可視化案例數(shù)據(jù)/地圖數(shù)據(jù)/疫情.txt","r",encoding="UTF-8")
content = f.read()
f.close()
data = json.loads(content)
province_data = data['areaTree'][0]['children']
# print(province_data)
map = Map()
Data = list()
for key in province_data:
province = tuple()
if key['name'] == "北京" or key['name'] == "天津" or key['name'] == "上海" or key['name'] == "重慶" :
province = (key['name']+"市" , key['total']['confirm'])
elif key['name'] == "內(nèi)蒙古" or key['name'] == "西藏" :
province = (key['name'] + "自治區(qū)", key['total']['confirm'])
elif key['name'] == "香港" or key['name'] == "澳門":
province = (key['name'] + "特別行政區(qū)", key['total']['confirm'])
elif key['name'] == "新疆":
province = (key['name'] + "維吾爾自治區(qū)", key['total']['confirm'])
elif key['name'] == "廣西":
province = (key['name'] + "壯族自治區(qū)", key['total']['confirm'])
elif key['name'] == "寧夏":
province = (key['name'] + "回族自治區(qū)", key['total']['confirm'])
else:
province = (key['name'] + "省", key['total']['confirm'])
Data.append(province)
print(Data)
map.add("各省份確診人數(shù)",Data,"china")
map.set_global_opts(
title_opts=TitleOpts(title="全國疫情地圖"),
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min": 1, "max": 99, "lable": "1~99", "color":"#CCFFFF"},
{"min": 100, "max": 999, "lable": "100~999", "color":"#FFFF99"},
{"min": 1000, "max": 4999, "lable": "1000~4999", "color":"#FF9966"},
{"min": 5000, "max": 9999, "lable": "5000~9999", "color":"#FF6666"},
{"min": 10000, "max": 99999, "lable": "10000~99999", "color":"#CC3333"},
{"min": 100000, "lable": "100000+", "color":"#990033"}
]
)
)
map.render("全國疫情地圖.html")