这就像在同一轴上绘制数据一样简单
- 拥有医疗机构的数据,然后获取这些机构的 GIS 数据
- 获取地图 GEOJSON 并在轴上绘图
- 在同一轴上散布数据,使用医疗机构类型作为颜色
import requests, io
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
# get some data of healthcare facilities
searchendpoint = "https://directory.spineservices.nhs.uk/ORD/2-0-0/organisations"
# get all healthcare facilities in Herefordshire
dfhc = pd.concat([pd.json_normalize(requests
.get(searchendpoint, params={"PostCode":f"HR{i}","Status":"Active"})
.json()["Organisations"])
for i in range(1,10)]).reset_index(drop=True)
# get geo data for postcodes
dfgeo = (pd.json_normalize(requests.post("http://api.postcodes.io/postcodes",
json={"postcodes":dfhc.PostCode.unique().tolist()[0:100]}).json()["result"])
.rename(columns={"result.postcode":"PostCode","result.longitude":"lng","result.latitude":"lat"})
.loc[:,["PostCode","lat","lng"]]
)
dfdata = dfhc.merge(dfgeo, on="PostCode", how="inner")
# going to use as color, so make if categorical so can get codes
dfdata["PrimaryRoleId"] = pd.Categorical(dfdata["PrimaryRoleId"])
fig, ax = plt.subplots(figsize=[14,6])
# get map of regions
df = gpd.read_file(io.StringIO(requests.get("https://martinjc.github.io/UK-GeoJSON/json/eng/msoa_by_lad/topo_E06000019.json").text))
df.plot(ax=ax)
# scatter data on top of region map
ax.scatter(x=dfdata["lng"],y=dfdata["lat"], s=50, c=dfdata["PrimaryRoleId"].cat.codes)
使用相同的数据集
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib
df = gpd.read_file("bgd_admbnda_adm2_bbs_20201113.shp")
fig, ax = plt.subplots(figsize=[8,8])
df.plot(ax=ax, alpha=0.5, edgecolor='k')
# some data that can be plotted on centroid
df["val"] = np.random.randint(1,100,len(df))
# use a discrete
cmap = plt.cm.get_cmap('jet', 5)
# scatter data based on co-ords of centroid
sc = ax.scatter(x=df.centroid.x, y=df.centroid.y, s=50, c=df["val"], cmap=cmap)
plt.colorbar(sc)