Issue
I want to plot a scatter plot over a map separated by divisions. So far I have tried the following.
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 has been found in github. It produces this figure.
Here, there are 8 divisions 'Barishal', 'Chattogram', 'Dhaka', 'Khulna', 'Rajshahi', 'Rangpur', 'Sylhet', 'Mymensingh'.
For every division, there are some numeric values(not latitude, longitude values). E.g. for Dhaka division [73.13 77.64 74.32 82.48 84.21 88.23 89.90]. For your convenience, I have attached the files in github.
Now, I split the values based on value range. E.g. i) 70-80: [73.13 77.64 74.32], ii) 80-90: [82.48 84.21 88.23 89.90]. Now, I want to draw a scatter plot of two categories of values on any places of Dhaka division with two colors such as this image. I have attached another expected output image for your reference .
Thanks in advance.
Solution
This is as simple as plotting data on same axis
- have data of healthcare facilities, then get GIS data for these facilities
- get map GEOJSON and plot on axis
- scatter data on same axis, using healthcare facility type as color
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)
Using same data set
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)
Answered By - Rob Raymond
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.