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

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

這里有個問題

is_show=True表示展示圖例,但是不準怎么辦?
這就需要手動校準范圍
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map=Map()
data=[
("北京",99),
("上海",199),
("湖南",299),
("臺灣",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é)果是

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


這樣就可以查詢相應的顏色
二.疫情地圖——國內(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()
#關閉文件
f.close()
#獲取各省數(shù)據(jù)
#將字符串json轉(zhuǎn)化為python的字典
data_dict=json.loads(data)
#從字典中取出省份數(shù)據(jù)
province_data_list=data_dict["areaTree"][0]["children"]
#組裝每個省份和確診人數(shù)為元組,并各個省的數(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)建地圖對象
map=Map()
#添加數(shù)據(jù)
map.add("各省份確診人數(shù)",data_list,"china")
#設置全局配置,定制分段到1視覺映射
map.set_global_opts(
title_opts=TitleOpts("全國疫情地圖",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("全國疫情地圖.html")
結(jié)果是

三.疫情地圖——省級疫情地圖
以河南省為例

代碼
import json
from pyecharts.charts import Map
from pyecharts.options import *
f=open("D:/疫情.txt","r",encoding="utf-8")
data=f.read()
#關閉文件
f.close()
#json數(shù)據(jù)轉(zhuǎn)化為python字典
data_dict=json.loads(data)
#取到河南省數(shù)據(jù)
cities_data=data_dict["areaTree"][0]["children"][3]["children"]
#準備數(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,"河南")
#設置全局選項
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é)果是

有個問題:濟源市因為數(shù)據(jù)集中沒有相應數(shù)據(jù),所以需要我們手動加上去

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

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

到此這篇關于python根據(jù)json數(shù)據(jù)畫疫情分布地圖的詳細代碼的文章就介紹到這了,更多相關python畫疫情分布地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
對python pandas中 inplace 參數(shù)的理解
這篇文章主要介紹了對python pandas中 inplace 參數(shù)的理解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
win10子系統(tǒng)python開發(fā)環(huán)境準備及kenlm和nltk的使用教程
這篇文章主要介紹了win10子系統(tǒng)python開發(fā)環(huán)境準備及kenlm和nltk的使用教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
用Python實現(xiàn)一個簡單的用戶系統(tǒng)
大家好,本篇文章主要講的是用Python實現(xiàn)一個簡單的用戶系統(tǒng),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Python中的遠程調(diào)試與性能優(yōu)化技巧分享
Python 是一種簡單易學、功能強大的編程語言,廣泛應用于各種領域,包括網(wǎng)絡編程、數(shù)據(jù)分析、人工智能等,在開發(fā)過程中,我們經(jīng)常會遇到需要遠程調(diào)試和性能優(yōu)化的情況,本文將介紹如何利用遠程調(diào)試工具和性能優(yōu)化技巧來提高 Python 應用程序的效率和性能2024-05-05

