python根據(jù)json數(shù)據(jù)畫疫情分布地圖的詳細(xì)代碼

注:數(shù)據(jù)集在文章最后
一.基礎(chǔ)地圖使用
1.掌握使用pyecharts構(gòu)建基礎(chǔ)的全國(guó)地圖可視化圖表
演示
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map=Map()
data=[
("北京",99),
("上海",199),
("湖南",299),
("臺(tái)灣",199),
("安徽",299),
("廣州",399),
("湖北",599)
]
map.add("地圖",data,"china")
map.set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True
)
)
map.render("1.html")結(jié)果是

這里有個(gè)問(wèn)題

is_show=True表示展示圖例,但是不準(zhǔn)怎么辦?
這就需要手動(dòng)校準(zhǔn)范圍
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map=Map()
data=[
("北京",99),
("上海",199),
("湖南",299),
("臺(tái)灣",199),
("安徽",299),
("廣州",399),
("湖北",599)
]
map.add("地圖",data,"china")
map.set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
{"min": 10, "max": 99, "label": "10-99人", "color": "#FFFF99"},
{"min": 100, "max": 499, "label": "100-499人", "color": "#FF9966"},
{"min": 500, "max": 999, "label": "500-999人", "color": "#FF6666"},
{"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CC3333"},
{"min": 10000, "label": "10000以上", "color": "#990033"},
]
)
)
map.render("1.html")結(jié)果是

這樣就可以了
再解釋一下顏色的設(shè)置


這樣就可以查詢相應(yīng)的顏色
二.疫情地圖——國(guó)內(nèi)疫情地圖
1.案例效果

演示

利用json在線在線解析工具可以看到

那么我們就可以知道該怎么去提取
#從字典中取出省份數(shù)據(jù) province_data_list=data_dict["areaTree"][0]["children"]
代碼
import json
from pyecharts.charts import Map
from pyecharts.options import *
#讀取文件
f=open("D:/疫情.txt","r",encoding="utf-8")
data=f.read()
#關(guān)閉文件
f.close()
#獲取各省數(shù)據(jù)
#將字符串json轉(zhuǎn)化為python的字典
data_dict=json.loads(data)
#從字典中取出省份數(shù)據(jù)
province_data_list=data_dict["areaTree"][0]["children"]
#組裝每個(gè)省份和確診人數(shù)為元組,并各個(gè)省的數(shù)據(jù)都封裝如列表
data_list=[]#繪圖需要用到數(shù)據(jù)列表
for province_data in province_data_list:
province_name=province_data["name"]#省份名稱
province_confirm=province_data["total"]["confirm"]#確診人數(shù)
data_list.append((province_name,province_confirm))#這里注意列表里面嵌套的是元組
print(f"{type(data_list)}\n{data_list}")
#創(chuàng)建地圖對(duì)象
map=Map()
#添加數(shù)據(jù)
map.add("各省份確診人數(shù)",data_list,"china")
#設(shè)置全局配置,定制分段到1視覺(jué)映射
map.set_global_opts(
title_opts=TitleOpts("全國(guó)疫情地圖",pos_left="center",pos_bottom="1%"),
visualmap_opts=VisualMapOpts(
is_show=True,#是否顯示
is_piecewise=True,#是否分段
pieces=[
{"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
{"min": 10, "max": 99, "label": "10-99人", "color": "#FFFF99"},
{"min": 100, "max": 499, "label": "100-499人", "color": "#FF9966"},
{"min": 500, "max": 999, "label": "500-999人", "color": "#FF6666"},
{"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CC3333"},
{"min": 10000, "label": "10000以上", "color": "#990033"},
]
)
)
map.render("全國(guó)疫情地圖.html")
結(jié)果是

三.疫情地圖——省級(jí)疫情地圖
以河南省為例

代碼
import json
from pyecharts.charts import Map
from pyecharts.options import *
f=open("D:/疫情.txt","r",encoding="utf-8")
data=f.read()
#關(guān)閉文件
f.close()
#json數(shù)據(jù)轉(zhuǎn)化為python字典
data_dict=json.loads(data)
#取到河南省數(shù)據(jù)
cities_data=data_dict["areaTree"][0]["children"][3]["children"]
#準(zhǔn)備數(shù)據(jù)為元組并放入list
data_list=[]
for city_data in cities_data:
city_name=city_data["name"]+"市"
city_confirm=city_data["total"]["confirm"]
data_list.append((city_name,city_confirm))
#構(gòu)建地圖
map=Map()
map.add("河南省疫情分布",data_list,"河南")
#設(shè)置全局選項(xiàng)
map.set_global_opts(
title_opts=TitleOpts(title="河南疫情地圖"),
visualmap_opts=VisualMapOpts(
is_show=True,#是否顯示
is_piecewise=True,#是否分段
pieces=[
{"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
{"min": 10, "max": 99, "label": "10-99人", "color": "#FFFF99"},
{"min": 100, "max": 499, "label": "100-499人", "color": "#FF9966"},
{"min": 500, "max": 999, "label": "500-999人", "color": "#FF6666"},
{"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CC3333"},
{"min": 10000, "label": "10000以上", "color": "#990033"},
]
)
)
map.render("河南疫情地圖.html")
結(jié)果是

有個(gè)問(wèn)題:濟(jì)源市因?yàn)閿?shù)據(jù)集中沒(méi)有相應(yīng)數(shù)據(jù),所以需要我們手動(dòng)加上去

這樣就可以了
結(jié)果是

四.數(shù)據(jù)集
鏈接: https://pan.baidu.com/s/10eqeAEPjZC9PohlSnMOkJg?pwd=sjte
提取碼: sjte

到此這篇關(guān)于python根據(jù)json數(shù)據(jù)畫疫情分布地圖的詳細(xì)代碼的文章就介紹到這了,更多相關(guān)python畫疫情分布地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python視頻爬蟲實(shí)現(xiàn)下載頭條視頻功能示例
這篇文章主要介紹了Python視頻爬蟲實(shí)現(xiàn)下載頭條視頻功能,涉及Python正則匹配、網(wǎng)絡(luò)傳輸及文件讀寫等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
對(duì)python pandas中 inplace 參數(shù)的理解
這篇文章主要介紹了對(duì)python pandas中 inplace 參數(shù)的理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
win10子系統(tǒng)python開(kāi)發(fā)環(huán)境準(zhǔn)備及kenlm和nltk的使用教程
這篇文章主要介紹了win10子系統(tǒng)python開(kāi)發(fā)環(huán)境準(zhǔn)備及kenlm和nltk的使用教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的用戶系統(tǒng)
大家好,本篇文章主要講的是用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的用戶系統(tǒng),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Python中的遠(yuǎn)程調(diào)試與性能優(yōu)化技巧分享
Python 是一種簡(jiǎn)單易學(xué)、功能強(qiáng)大的編程語(yǔ)言,廣泛應(yīng)用于各種領(lǐng)域,包括網(wǎng)絡(luò)編程、數(shù)據(jù)分析、人工智能等,在開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到需要遠(yuǎn)程調(diào)試和性能優(yōu)化的情況,本文將介紹如何利用遠(yuǎn)程調(diào)試工具和性能優(yōu)化技巧來(lái)提高 Python 應(yīng)用程序的效率和性能2024-05-05
python進(jìn)階學(xué)習(xí)實(shí)時(shí)目標(biāo)跟蹤示例詳解
這篇文章主要為大家介紹了python進(jìn)階學(xué)習(xí)實(shí)時(shí)目標(biāo)跟蹤示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

