如何在由分区分隔的地图上绘制散点图?
How to plot a scatter plot over a map separated by divisions?
我想在以分区分隔的地图上绘制散点图。到目前为止,我已经尝试了以下方法。
import os
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
import numpy as np
fPath = 'shape/bgd_adm_bbs_20201113_SHP/bgd_admbnda_adm2_bbs_20201113.shp'
bgd = gpd.read_file(fPath)
ax = bgd.plot(figsize=(15,15),column='ADM1_EN', legend=True)
bgd_admbnda_adm2_bbs_20201113.shp 已在 github. It produces this figure.
中找到
Here, there are 8 divisions 'Barishal', 'Chattogram', 'Dhaka', 'Khulna', 'Rajshahi', 'Rangpur', 'Sylhet', 'Mymensingh'.
对于每个分区,都有一些数值(不是纬度、经度值)。例如。 达卡分区 [73.13 77.64 74.32 82.48 84.21 88.23 89.90]。为了您的方便,我附上了 github 中的文件。
现在,我根据值范围拆分值。例如。 i) 70-80: [73.13 77.64 74.32], ii) 80-90: [82.48 84.21 88.23 89.90]。现在,我想用两种颜色在达卡分区的任何地方绘制两类值的散点图,例如this image. I have attached another expected output image for your reference 。
提前致谢。
这就像在同一轴上绘制数据一样简单
- 拥有医疗机构的数据,然后获取这些机构的 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)
我想在以分区分隔的地图上绘制散点图。到目前为止,我已经尝试了以下方法。
import os
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
import numpy as np
fPath = 'shape/bgd_adm_bbs_20201113_SHP/bgd_admbnda_adm2_bbs_20201113.shp'
bgd = gpd.read_file(fPath)
ax = bgd.plot(figsize=(15,15),column='ADM1_EN', legend=True)
bgd_admbnda_adm2_bbs_20201113.shp 已在 github. It produces this figure.
中找到Here, there are 8 divisions 'Barishal', 'Chattogram', 'Dhaka', 'Khulna', 'Rajshahi', 'Rangpur', 'Sylhet', 'Mymensingh'.
对于每个分区,都有一些数值(不是纬度、经度值)。例如。 达卡分区 [73.13 77.64 74.32 82.48 84.21 88.23 89.90]。为了您的方便,我附上了 github 中的文件。
现在,我根据值范围拆分值。例如。 i) 70-80: [73.13 77.64 74.32], ii) 80-90: [82.48 84.21 88.23 89.90]。现在,我想用两种颜色在达卡分区的任何地方绘制两类值的散点图,例如this image. I have attached another expected output image for your reference
提前致谢。
这就像在同一轴上绘制数据一样简单
- 拥有医疗机构的数据,然后获取这些机构的 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)