I would like to plot a trajectory on a Basemap, and have country labels (names) shown as an overlay.
Here is the current code and the map it produces:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
path = "path\\to\\data"
animal_data = pd.DataFrame.from_csv(path, header=None)
animal_data.columns = ["date", "time", "gps_lat", "gps_long"]
# data cleaning omitted for clarity
params = {
'projection':'merc',
'lat_0':animal_data.gps_lat.mean(),
'lon_0':animal_data.gps_long.mean(),
'resolution':'h',
'area_thresh':0.1,
'llcrnrlon':animal_data.gps_long.min()-10,
'llcrnrlat':animal_data.gps_lat.min()-10,
'urcrnrlon':animal_data.gps_long.max()+10,
'urcrnrlat':animal_data.gps_lat.max()+10
}
map = Basemap(**params)
map.drawcoastlines()
map.drawcountries()
map.fillcontinents(color = 'coral')
map.drawmapboundary()
x, y = map(animal_data.gps_long.values, animal_data.gps_lat.values)
map.plot(x, y, 'b-', linewidth=1)
plt.show()
This results in the map:
This is a map of the trajectory of a migrating bird. While this is a very nice map (!), I need country-name labels so it is easy to determine the countries the bird is flying through.
Is there a straight-forward way of adding the country names?
My solution relies on an external data file that may or may not be available in the future. However, similar data can be found elsewhere, so that should not be too much of a problem.
First, the code for printing the country-name labels:
All this does is to download a country-location database from here, then select countries that are currently on the map, and label them.
The complete code:
and finally, the result:
Clearly this isn't as carefully labeled as one might hope, and some heuristics regarding country size, name length and map size should be implemented to make this perfect, but this is a good starting point.