gpt4 book ai didi

pandas - 如何在由分区分隔的 map 上绘制散点图?

转载 作者:行者123 更新时间:2023-12-04 07:49:59 27 4
gpt4 key购买 nike

我想在由分区分隔的 map 上绘制散点图。到目前为止,我已经尝试了以下方法。

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 中找到.它产生这个 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 .我附上了另一个 预期输出图像 供您引用 Expected output .
提前致谢。

最佳答案

这就像在同一轴上绘制数据一样简单

  • 拥有医疗机构的数据,然后获取这些机构的 GIS 数据
  • 获取 map 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)

    enter image description here

    关于pandas - 如何在由分区分隔的 map 上绘制散点图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67033501/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com