Search code examples
pythonpandasmatplotlibcartopy

How do I plot coordinates in csv on the map from from 'natural_earth" using cartopy and matplotlib?


I have successfully created a map (country boundary and coastline) from natural earth site but I am finding it difficult to plot the longitude and latitude coordinates of weather some stations to the map. The longitude and latitude coordinates are the CSV file attached.

Below the is code compiled so far and the map generated:

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

[enter image description here][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()

How do I plot the coordinates on the CSV file on the map generated, please? Thanks

Image description: [https://i.sstatic.net/07vzp.png]

link to CSV file: [https://drive.google.com/file/d/152UTebTc_sDbyKDXV3g52jYiVG4n6LEx/view?usp=sharing]


Solution

  • This requires two steps

    1. Read in the csv data to Python. You can do this with numpy or pandas, e.g. weather_stations = pd.read_csv('path_to_file.csv')

    2. Use the matplotlib function scatter on your geoaxes ax_map. You need to tell the geoaxes the coordinate reference system of your input data. It looks like lons and lats, this is the Plate Carree coordinate reference system. You pass this with the kwarg transform

    Taking the data we imported in step 1:

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