一、利用geojson绘制分层填色分布地图
上一篇文章介绍了利用geojson绘制地图,下面介绍用geojson绘制分层填色地图。首先,从百度疫情实时大数据报告网站下载疫情数据(网址为:
https://voice.baidu.com/act/newpneumonia/newpneumonia?fraz=partner&paaz=gjyj),从网站http://datav.aliyun.com/tools/atlas/#&lat=33.521903996156105&lng=104.29849999999999&zoom=3下载geojson中国地图数据。
导入地图数据。
import geojson as json
json_data=json.load(open("d:/中华人民共和国.json",encoding='utf-8'))
boundary_data=json.load(open("d:/中华人民共和国1.json",encoding='utf-8'))
读入疫情数据。
import pandas as pd
data=pd.read_csv("d:/name.csv",encoding="gb18030")
下面绘制分层填色分布地图。
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
norm = plt.Normalize(data["死亡"].min(), data["死亡"].max())
map_vir = plt.cm.get_cmap(name='plasma')
fig=plt.figure(figsize=(8,10))
ax=fig.add_subplot()
ax.set_xlim(70,140)
ax.set_ylim(0,57)
poly=json_data['features']
for i in range(0,len(poly)):
try:
po=poly[i]['geometry']["coordinates"]
norm_values = norm(data["死亡"][i])
color = map_vir(norm_values)
lc = PolyCollection(po[0], edgecolor='grey', facecolor=color, closed=False)
ax.add_collection(lc)
except(ValueError):
po=poly[i]['geometry']["coordinates"]
norm_values = norm(data["死亡"][i])
color = map_vir(norm_values)
lc = PolyCollection(po, edgecolor='grey', facecolor=color, closed=False)
ax.add_collection(lc)
except:
pass
#绘制地图边界
boundary_poly= boundary_data [4]['geometry']["coordinates"]
boundary_lc = PolyCollection(boundary_poly, edgecolor='red',facecolor='none', closed=False)
ax.add_collection(boundary_lc)
#绘制图例
sm = plt.cm.ScalarMappable(norm=norm,cmap=map_vir)
fig.colorbar(sm,norm=norm,ax=ax)
#显示地图
plt.show()
新冠疫情死亡人数分布地图
二、绘制三维数据地图
我们用分层填色地图表示新冠导致死亡的人数,三维柱状图表示新冠确诊人数。绘制的代码和效果如下。
fig=plt.figure(figsize=(8,10))
ax=fig.add_subplot(projection='3d')
ax.set_xlim(70,140)
ax.set_ylim(0,57)
poly=json_data['features']
for i in range(0,len(poly)):
try:
po=poly[i]['geometry']["coordinates"]
norm_values = norm(data["死亡"][i])
color = map_vir(norm_values)
lc = PolyCollection(po[0], edgecolor='grey', facecolor=color, closed=False)
ax. add_collection3d(lc)
except(ValueError):
po=poly[i]['geometry']["coordinates"]
norm_values = norm(data["死亡"][i])
color = map_vir(norm_values)
lc = PolyCollection(po, edgecolor='grey', facecolor=color, closed=False)
ax. add_collection3d(lc)
except:
pass
绘制三维BAR图
for i in range(0,len(data["累计"])):
ax.bar3d(
gdata.representative_point().x[i],gdata.representative_point().y[i],0,0.5,0.5, data["累计"][i],color= "red")
#plt.axis('off')
#显示数据地图
plt.show()