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

Python中如何使用美国邮政编码绘制分类地图与计数热力图

geopandas是实现该需求的合适工具,以下是对应问题的解决方案:

1. 缩小地图范围至美国

修改现有代码中读取地图和绘制的部分,从全球矢量数据中过滤出美国范围,还可以手动限制坐标范围只显示美国本土:

# 原有代码不变,仅修改地图绘制部分逻辑
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# 过滤美国边界
us = world[world.name == 'United States of America']
ax = us.plot(figsize=(10, 6), color='#f0f0f0', edgecolor='gray')
# 限制坐标范围为美国本土,删除这两行即可显示阿拉斯加、夏威夷等区域
ax.set_xlim(-125, -65)
ax.set_ylim(25, 50)

# 绘制点时增加column参数按colC自动区分颜色,开启图例
gdf.plot(
    ax=ax, 
    marker='o', 
    column='colC', 
    legend=True, 
    markersize=15,
    edgecolor='white'
)
plt.savefig('us_zip_points.jpg')

2. 生成基于count的热力图

可以搭配seaborn的核密度图实现,权重关联count列即可体现热力强度:

import seaborn as sns

ax = us.plot(figsize=(10, 6), color='white', edgecolor='black')
ax.set_xlim(-125, -65)
ax.set_ylim(25, 50)

# 绘制核密度热力图
sns.kdeplot(
    x=gdf['Longitude'],
    y=gdf['Latitude'],
    weights=gdf['count'],
    fill=True,
    cmap='YlOrRd',
    alpha=0.6,
    ax=ax
)

# 可选:叠加colC分类的点方便对应
gdf.plot(
    ax=ax, 
    marker='o', 
    column='colC', 
    legend=True, 
    markersize=gdf['count']*10, # 点大小也关联count值
    edgecolor='black'
)
plt.savefig('us_zip_heatmap.jpg')

3. 邮编边界获取说明

如果需要展示每个邮编的多边形区域热力,可直接下载美国人口普查局发布的邮编边界shape文件,用gpd.read_file()读取后,和你的数据按zipcode字段关联,即可基于面绘制分区热力。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 09:15:01