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

如何用Cartopy和Matplotlib将CSV站点坐标绘入Natural Earth尼日利亚地图?

如何将CSV中的气象站点坐标添加到Cartopy地图上

我来帮你搞定这个问题!要把CSV文件里的气象站点经纬度添加到你已经创建的尼日利亚地图上,只需要在现有代码基础上添加几行读取CSV和绘制散点的代码就行。下面是完整的解决方案:

步骤说明

  • 首先确保你安装了pandas(用来快速读取CSV文件),如果没装的话可以用命令:pip install pandas
  • 读取CSV文件,提取里面的经度和纬度数据
  • 在已有的Cartopy地图轴上绘制这些坐标点,注意指定正确的坐标系

修改后的完整代码

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import cartopy.io.shapereader as shapereader
import pandas as pd  # 新增:导入pandas用于读取CSV

# 读取尼日利亚边界
countries = shapereader.natural_earth(resolution='10m', category='cultural', name='admin_0_countries')
for country in shapereader.Reader(countries).records():
    if country.attributes['SU_A3'] == 'NGA':
        nigeria = country.geometry
        break
else:
    raise ValueError('Unable to find the NGA boundary.')

# 读取CSV中的气象站点数据(替换成你本地的CSV文件路径)
stations_df = pd.read_csv("你的气象站点文件.csv")
# 假设CSV里的经度列名为"Longitude",纬度列名为"Latitude",如果你的列名不同请修改这里
lon = stations_df['Longitude']
lat = stations_df['Latitude']

# 创建地图
plt.figure(figsize=(10, 5))
ax_map = plt.axes(projection=ccrs.PlateCarree())
ax_map.set_extent([-1, 19, -1, 17], ccrs.PlateCarree())
ax_map.add_feature(feature.COASTLINE, linewidth=.5)
ax_map.add_geometries([nigeria], ccrs.Geodetic(), edgecolor='0.8', facecolor='none')
grid_lines = ax_map.gridlines(draw_labels=True)

# 新增:绘制气象站点散点
ax_map.scatter(lon, lat, transform=ccrs.PlateCarree(), 
               marker='o', color='#ff3333', s=60, alpha=0.8, label='气象站点')
# 添加图例
ax_map.legend()

plt.show()

关键要点解释

  • 读取CSV:用pd.read_csv()可以快速解析CSV文件,如果你不想用pandas,也可以用Python内置的csv模块,不过pandas更简洁。
  • 坐标系指定:transform=ccrs.PlateCarree()是必须的,因为你的CSV里的经纬度属于WGS84坐标系(也就是PlateCarree投影),Cartopy需要知道数据的坐标系才能正确映射到地图上。
  • 标记美化:通过marker、color、s(大小)、alpha(透明度)这些参数可以让站点标记更显眼,方便观察。

如果你的CSV文件里的经纬度列名不是"Longitude"和"Latitude",记得修改代码中对应的列名哦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:00:30