python如何繪制疫情圖
python中進(jìn)行圖表繪制的庫主要有兩個(gè):matplotlib 和 pyecharts, 相比較而言:
matplotlib中提供了BaseMap可以用于地圖的繪制,但是個(gè)人覺得其繪制的地圖不太美觀,而且安裝相較而言有點(diǎn)麻煩。
pyecharts是基于百度開源的js庫echarts而來,其最大的特點(diǎn)是:安裝簡單、使用也簡單。
所以決定使用pyecharts來繪制地圖。
1.安裝pyecharts
如果有anaconda環(huán)境,可用 pip install pyecharts 命令安裝pyecharts。
由于我們要繪制中國的疫情地圖,所以還要額外下載幾個(gè)地圖。地圖文件被分成了三個(gè)Python包,分別為:
全球國家地圖: echarts-countries-pypkg
安裝命令:pip install echarts-countries-pypkg
中國省級地圖: echarts-china-provinces-pypkg
安裝命令:pip install echarts-china-provinces-pypkg
中國市級地圖: echarts-china-cities-pypkg
安裝命令:pip install echarts-china-cities-pypkg


2.導(dǎo)包。
繪制地圖時(shí)我們根據(jù)自己需要導(dǎo)入需要的包,在pyecharts的官方文檔 https://pyecharts.org/#/ 中詳細(xì)列出了繪制各種圖表的的方法及參數(shù)含義,而且提供了各種圖標(biāo)的demo,方便我們更好地使用pyecharts。
from pyecharts.charts import Map from pyecharts import options as opts
3.代碼
# 用于保存城市名稱和確診人數(shù)
map_data = []
for i in china :
print(i)
# 獲得省份名稱
province = i["name"]
print("province:",province)
province_confirm = i["total"]["confirm"]
# 保存省份名稱和該省確診人數(shù)
map_data.append((i["name"],province_confirm))
c = (
# 聲明一個(gè)map對象
Map()
# 添加數(shù)據(jù)
.add("確診", map_data, "china")
# 設(shè)置標(biāo)題和顏色
.set_global_opts(title_opts=opts.TitleOpts(title="全國疫情圖"),
visualmap_opts=opts.VisualMapOpts(split_number=6,is_piecewise=True,
pieces=[{"min":1,"max":9,"label":"1-9人","color":"#ffefd7"},
{"min":10,"max":99,"label":"10-99人","color":"#ffd2a0"},
{"min":100,"max":499,"label":"100-499人","color":"#fe8664"},
{"min":500,"max":999,"label":"500-999人","color":"#e64b47"},
{"min":1000,"max":9999,"label":"1000-9999人","color":"#c91014"},
{"min":10000,"label":"10000人及以上","color":"#9c0a0d"}
]))
)
# 生成html文件
c.render("全國實(shí)時(shí)疫情.html")
運(yùn)行成功后就可以在工程目錄下發(fā)現(xiàn)一個(gè)名為“全國實(shí)時(shí)疫情”的html文件,打開就可以看到我們繪制的疫情圖啦??!

全部代碼(包含保存到數(shù)據(jù)庫,爬取數(shù)據(jù)、繪制疫情圖):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import requests
import pymysql
# 裝了anaconda的可以pip install pyecharts安裝pyecharts
from pyecharts.charts import Map,Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType,RenderType
# 繪圖包參加網(wǎng)址https://pyecharts.org/#/zh-cn/geography_charts
id = 432
coon = pymysql.connect(user='root', password='root', host='127.0.0.1', port=3306, database='yiqing',use_unicode=True, charset="utf8")
cursor = coon.cursor()
url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
resp=requests.get(url)
html=resp.json()
data=json.loads(html["data"])
time = data["lastUpdateTime"]
data_info = time.split(' ')[0]
detail_time = time.split(' ')[1]
# 獲取json數(shù)據(jù)的全國省份疫情情況數(shù)據(jù)
china=data["areaTree"][0]["children"]
# 用于保存城市名稱和確診人數(shù)
map_data = []
for i in china :
print(i)
# 獲得省份名稱
province = i["name"]
print("province:",province)
province_confirm = i["total"]["confirm"]
# 保存省份名稱和該省確診人數(shù)
map_data.append((i["name"],province_confirm))
# 各省份下有各市,獲取各市的疫情數(shù)據(jù)
for child in i["children"]:
print(child)
# 獲取城市名稱
city = child["name"]
print("city:",city)
# 獲取確診人數(shù)
confirm = int(child["total"]["confirm"])
# 獲取疑似人數(shù)
suspect = int(child["total"]["suspect"])
# 獲取死亡人數(shù)
dead = int(child["total"]["dead"])
# 獲取治愈人數(shù)
heal = int(child["total"]["heal"])
# 插入數(shù)據(jù)庫中
cursor.execute("INSERT INTO city(id,city,confirm,suspect,dead,heal,province,date_info,detail_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)",
(id, city, confirm, suspect, dead, heal, province, data_info, detail_time))
id = id + 1
coon.commit()
c = (
# 聲明一個(gè)map對象
Map()
# 添加數(shù)據(jù)
.add("確診", map_data, "china")
# 設(shè)置標(biāo)題和顏色
.set_global_opts(title_opts=opts.TitleOpts(title="全國疫情圖"),
visualmap_opts=opts.VisualMapOpts(split_number=6,is_piecewise=True,
pieces=[{"min":1,"max":9,"label":"1-9人","color":"#ffefd7"},
{"min":10,"max":99,"label":"10-99人","color":"#ffd2a0"},
{"min":100,"max":499,"label":"100-499人","color":"#fe8664"},
{"min":500,"max":999,"label":"500-999人","color":"#e64b47"},
{"min":1000,"max":9999,"label":"1000-9999人","color":"#c91014"},
{"min":10000,"label":"10000人及以上","color":"#9c0a0d"}
]))
)
# 生成html文件
c.render("全國實(shí)時(shí)疫情.html")
#
# china_total="確診" + str(data["chinaTotal"]["confirm"])+ "疑似" + str(data["chinaTotal"]["suspect"])+ "死亡" + str(data["chinaTotal"]["dead"]) + "治愈" + str(data["chinaTotal"]["heal"]) + "更新日期" + data["lastUpdateTime"]
# print(china_total)
以上就是python如何繪制疫情圖的詳細(xì)內(nèi)容,更多關(guān)于python繪制疫情圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置
這篇文章主要介紹了使用Python獲取當(dāng)前工作目錄和執(zhí)行命令的位置,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python-pandas創(chuàng)建Series數(shù)據(jù)類型的操作
這篇文章主要介紹了python-pandas創(chuàng)建Series數(shù)據(jù)類型的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Python在for循環(huán)里處理大數(shù)據(jù)的推薦方法實(shí)例
這篇文章主要介紹了Python在for循環(huán)里處理大數(shù)據(jù)的推薦方法實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python實(shí)現(xiàn)的樸素貝葉斯算法經(jīng)典示例【測試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)的樸素貝葉斯算法,結(jié)合實(shí)例形式詳細(xì)分析了Python實(shí)現(xiàn)與使用樸素貝葉斯算法的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
OpenCV實(shí)現(xiàn)機(jī)器人對物體進(jìn)行移動跟隨的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于OpenCV實(shí)現(xiàn)機(jī)器人對物體進(jìn)行移動跟隨的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python3中PyQt5簡單實(shí)現(xiàn)文件打開及保存
本文將結(jié)合實(shí)例代碼,介紹Python3中PyQt5簡單實(shí)現(xiàn)文件打開及保存,具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06

