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

如何在GeoPandas中通过指定经纬度裁剪绘图地图?

用GeoPandas按指定经纬度裁剪地图的方法

你可以通过两种实用方式实现经纬度范围的地图裁剪,以下是具体操作:

方法1:直接设置坐标轴显示范围(快速调整视图)

这种方式无需修改原始数据,仅限制绘图的显示区域,适合快速预览指定区域:

import geopandas as gpd
import matplotlib.pyplot as plt

countries = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
brazil = countries[countries["name"] == "Brazil"]

fig, ax = plt.subplots(figsize=(12,6))
brazil.plot(color="lightgrey", ax=ax)

# 自定义经纬度范围:[最小经度, 最大经度, 最小纬度, 最大纬度]
ax.set_xlim(-75, -30)  # 设置经度范围
ax.set_ylim(-35, 5)    # 设置纬度范围
plt.show()

方法2:空间裁剪GeoDataFrame(真正保留范围内数据)

如果需要筛选出指定经纬度范围内的几何要素,可使用GeoPandas内置的cx索引器,实现数据层面的裁剪:

import geopandas as gpd
import matplotlib.pyplot as plt

countries = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
brazil = countries[countries["name"] == "Brazil"]

# 定义裁剪范围:格式为 cx[经度范围, 纬度范围],范围用min:max表示
cropped_brazil = brazil.cx[-75:-30, -35:5]

fig, ax = plt.subplots(figsize=(12,6))
cropped_brazil.plot(color="lightgrey", ax=ax)
plt.show()

注意事项

  • 若你的数据坐标系不是WGS84(EPSG:4326,即常规经纬度坐标系),需要先转换坐标系:brazil = brazil.to_crs(epsg=4326),确保经纬度范围与数据匹配。
  • cx索引器支持负数范围,对应西经、南纬区域。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:03:10