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

如何仅用单一纬度或经度在地图上绘制线条?附点位绘制代码

基于单一纬度/经度绘制线条的实现方案

当然可以仅基于单一纬度或经度在地图上绘制线条,核心是利用GeoPandas的LineString类型定义线条几何,再和底图叠加绘制。以下是具体实现步骤:

前提准备

确保你已导入所需依赖(和现有代码的环境一致):

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, LineString

1. 绘制单一纬度的横向线条

比如要绘制北纬30°的水平线,只需构造一条覆盖目标经度范围的线条即可:

# 设定目标纬度值
target_lat = 30
# 构造跨全球经度的LineString(西经180°到东经180°)
lat_line = LineString([(-180, target_lat), (180, target_lat)])
# 转为GeoDataFrame并指定坐标系
lat_line_gdf = gpd.GeoDataFrame(geometry=[lat_line], crs="EPSG:4326")

# 整合到你的现有绘图流程
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
ax = world.plot(figsize=(10, 6))

# 保留原有点位绘制代码
df = pd.read_csv("geo.csv", delimiter=';', skiprows=0, low_memory=False)
geometry = [Point(xy) for xy in zip(df['longitude'], df['latitude'])]
gdf = gpd.GeoDataFrame(df, geometry=geometry)
gdf.plot(ax=ax, marker='o', color='red', markersize=15)

# 绘制纬度线条
lat_line_gdf.plot(ax=ax, color='blue', linewidth=2)

2. 绘制单一经度的纵向线条

比如要绘制东经120°的垂直线,构造覆盖目标纬度范围的线条即可:

# 设定目标经度值
target_lon = 120
# 构造跨全球纬度的LineString(南纬90°到北纬90°)
lon_line = LineString([(target_lon, -90), (target_lon, 90)])
# 转为GeoDataFrame并指定坐标系
lon_line_gdf = gpd.GeoDataFrame(geometry=[lon_line], crs="EPSG:4326")

# 整合到现有绘图流程
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
ax = world.plot(figsize=(10, 6))

# 保留原有点位绘制代码
df = pd.read_csv("geo.csv", delimiter=';', skiprows=0, low_memory=False)
geometry = [Point(xy) for xy in zip(df['longitude'], df['latitude'])]
gdf = gpd.GeoDataFrame(df, geometry=geometry)
gdf.plot(ax=ax, marker='o', color='red', markersize=15)

# 绘制经度线条
lon_line_gdf.plot(ax=ax, color='green', linewidth=2)

可选:自定义线条起止范围

如果不需要覆盖全球,只需修改LineString的坐标即可,比如绘制北纬30°从东经0°到东经120°的线段:

custom_lat_line = LineString([(0, 30), (120, 30)])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 21:55:20