如何使用cartopy和matplotlib从“自然地球”在地图上绘制csv坐标?

2024-04-25 13:20:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地从自然地球站点创建了一张地图(国家边界和海岸线),但我发现很难在地图上绘制一些气象站的经纬度坐标。经度和纬度坐标是附加的CSV文件

下面是迄今为止编译的代码和生成的映射:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import cartopy.io.shapereader as shapereader

[在此处输入图像描述][1]

countries = shapereader.natural_earth(resolution='10m',
                                      category='cultural',
                                      name='admin_0_countries')

# Find the Nigeria boundary polygon.
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.')

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)

plt.show()

如何在生成的地图上绘制CSV文件上的坐标?谢谢

图像描述: [https://i.stack.imgur.com/07vzp.png]

链接到CSV文件:[https://drive.google.com/file/d/152UTebTc_sDbyKDXV3g52jYiVG4n6LEx/view?usp=sharing]


Tags: 文件csvimportmapas地图绘制plt
1条回答
网友
1楼 · 发布于 2024-04-25 13:20:10

这需要两个步骤

  1. 将csv数据读入Python。您可以使用numpypandas执行此操作,例如weather_stations = pd.read_csv('path_to_file.csv')

  2. 在geoaxes ax_map上使用matplotlib函数scatter。您需要告诉geoaxes输入数据的坐标参考系。它看起来像lons和lats,这是平板卡里坐标系。您通过kwargtransform传递这个

以我们在步骤1中导入的数据为例:

ax_map.scatter(weather_stations['LONG'], weather_stations['LAT'], transform=ccrs.PlateCarree())

相关问题 更多 >