You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在px.scatter_geo图表中添加佐治亚州(GA)县界

解决方案

一、添加佐治亚州县界

你可以通过叠加Plotly图形对象图层的方式,将GeoJSON/Shapefile格式的县界添加到散点地图上,具体步骤如下:

  1. 用GeoPandas读取你的县界文件(GeoJSON或Shapefile均支持):

    # 读取GeoJSON(如果是Shapefile,直接传入.shp文件路径即可)
    ga_counties = geopandas.read_file("/content/ga_counties.geojson")
    
  2. 将GeoDataFrame转换为Plotly可用的地理线数据,通过add_trace添加到现有散点图中:
    遍历每个县的边界坐标,创建线条图层并设置样式:

    # 遍历每个县的边界,添加县界线条
    for _, row in ga_counties.iterrows():
        # 处理多边形/多类型多边形的坐标提取
        if row.geometry.type == 'Polygon':
            lons, lats = row.geometry.exterior.coords.xy
        elif row.geometry.type == 'MultiPolygon':
            lons = []
            lats = []
            for poly in row.geometry.geoms:
                poly_lons, poly_lats = poly.exterior.coords.xy
                lons.extend(poly_lons)
                lats.extend(poly_lats)
                lons.append(None)  # 用空值分隔不同子多边形
                lats.append(None)
        # 添加县界图层
        fig.add_trace(go.Scattergeo(
            lon=lons,
            lat=lats,
            mode='lines',
            line=dict(width=1, color='gray'),  # 自定义县界的宽度和颜色
            hoverinfo='none'  # 悬浮时不显示县界相关信息
        ))
    

二、移除悬浮注释中的经纬度

你只需要在hover_data参数里明确将lat和lng设置为False,即可隐藏它们同时保证代码正常运行(px.scatter_geo仍需要这两个字段定位散点,只是不在悬浮框显示):

修改你的px.scatter_geo调用部分:

fig = px.scatter_geo(df, lat='lat', lon='lng', color='county',
                     hover_name='county', scope='usa',
                     hover_data={
                     'lat': False,  # 隐藏纬度
                     'lng': False,  # 隐藏经度
                     '2019 New Residential Units': True,
                     '2019 Total Valuation': True,
                     '2020 New Residential Units': True,
                     '2020 Total Valuation': True,
                     '2021 New Residential Units': True,
                     '2021 Total Valuation': True,
                     '2022 New Residential Units': True,
                     '2022 Total Valuation': True,
                     'population': True,
                     },
                     center=dict(lat=32.1574, lon=-82.9071))

完整修改后的绘图代码片段

将上述修改整合后,最终的绘图逻辑如下:

# 读取县界文件(替换为你的实际文件路径)
ga_counties = geopandas.read_file("/content/ga_counties.geojson")

fig = px.scatter_geo(df, lat='lat', lon='lng', color='county',
                     hover_name='county', scope='usa',
                     hover_data={
                     'lat': False,
                     'lng': False,
                     '2019 New Residential Units': True,
                     '2019 Total Valuation': True,
                     '2020 New Residential Units': True,
                     '2020 Total Valuation': True,
                     '2021 New Residential Units': True,
                     '2021 Total Valuation': True,
                     '2022 New Residential Units': True,
                     '2022 Total Valuation': True,
                     'population': True,
                     },
                     center=dict(lat=32.1574, lon=-82.9071))

# 添加县界图层
for _, row in ga_counties.iterrows():
    if row.geometry.type == 'Polygon':
        lons, lats = row.geometry.exterior.coords.xy
    elif row.geometry.type == 'MultiPolygon':
        lons = []
        lats = []
        for poly in row.geometry.geoms:
            poly_lons, poly_lats = poly.exterior.coords.xy
            lons.extend(poly_lons)
            lats.extend(poly_lats)
            lons.append(None)
            lats.append(None)
    fig.add_trace(go.Scattergeo(
        lon=lons,
        lat=lats,
        mode='lines',
        line=dict(width=1, color='gray'),
        hoverinfo='none'
    ))

fig.update_geos(fitbounds="locations")
fig.update_layout(height=600, margin={"r":0,"t":0,"l":0,"b":0})
fig.write_html('permitvaluationplot.html')
fig.show()

内容的提问来源于stack exchange,提问作者C Sicily

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 18:41:13