python - Country labels on Basemap -


i plot trajectory on basemap, , have country labels (names) shown overlay.

here current code , map produces:

import pandas pd  import matplotlib.pyplot plt  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 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 map: migration

this map of trajectory of migrating bird. while nice map (!), need country-name labels easy determine countries bird flying through.

is there straight-forward way of adding country names?

my solution relies on external data file may or may not available in future. however, similar data can found elsewhere, should not of problem.

first, code printing country-name labels:

import pandas pd  import matplotlib.pyplot plt  mpl_toolkits.basemap import basemap  class mybasemap(basemap):          def printcountries(self, d=3, max_len=12):         data = pd.io.parsers.read_csv("http://opengeocode.org/cude/download.php?file=/home/fashions/public_html/opengeocode.org/download/cow.txt",                                        sep=";", skiprows=28 )         data = data[(data.latitude > self.llcrnrlat+d) & (data.latitude < self.urcrnrlat-d) & (data.longitude > self.llcrnrlon+d) & (data.longitude < self.urcrnrlon-d)]         ix, country in data.iterrows():                                             plt.text(*self(country.longitude, country.latitude), s=country.bgn_name[:max_len])  

all download country-location database here, select countries on map, , label them.

the complete code:

import pandas pd  import matplotlib.pyplot plt  mpl_toolkits.basemap import basemap  class mybasemap(basemap):          def printcountries(self, d=3, max_len=12):         data = pd.io.parsers.read_csv("http://opengeocode.org/cude/download.php?file=/home/fashions/public_html/opengeocode.org/download/cow.txt",                                        sep=";", skiprows=28 )         data = data[(data.latitude > self.llcrnrlat+d) & (data.latitude < self.urcrnrlat-d) & (data.longitude > self.llcrnrlon+d) & (data.longitude < self.urcrnrlon-d)]         ix, country in data.iterrows():                                             plt.text(*self(country.longitude, country.latitude), s=country.bgn_name[:max_len])    path = "path\\to\\data"  animal_data = pd.dataframe.from_csv(path, header=none) animal_data.columns = ["date", "time", "gps_lat", "gps_long"]  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 }  plt.figure() map = mybasemap(**params)  map.drawcoastlines() map.fillcontinents(color = 'coral') map.drawmapboundary()           map.drawcountries() map.printcountries()  x, y = map(animal_data.gps_long.values, animal_data.gps_lat.values)  map.plot(x, y, 'b-', linewidth=1)     plt.show() 

and finally, result:

labeld-map

clearly isn't labeled 1 might hope, , heuristics regarding country size, name length , map size should implemented make perfect, starting point.


Comments

Popular posts from this blog

c# - Where does the .ToList() go in LINQ query result -

Listeners to visualise results of load test in JMeter -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -