Basemap is among the most popular Matplotlib-based plotting toolkits to plot over world maps. It is a handy way to show any geographical location.
To install Basemap, do this:
- Unpack in $Python3_dir/site-packages/mpl_toolkits
- Enter the Basemap installation directory: cd $basemap_dir
- Enter the geos directory in the Basemap directory: cd $basemap/geos-3.3.3
- Install the GEOS library via ./configure, make, and make install
- Install PyProj (refer to the following tip)
- Return to the Basemap installation directory and run python3 setup.py install
- Set the environment variable PROJ_DIR=$pyproj_dir/lib/pyproj/data
- Clone the PyProj GitHub repository from https://github.com/jswhit/pyproj into the Python site packages directory
- Install the Cython dependency with pip install --user cython
- Enter the PyProj directory and install using python3 setup.py install
As a brief introduction, we will show how to draw our beautiful Earth with shaded terrain with the following code snippet:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# Initialize a Basemap object
# Use orthogonal spherical projection
# Adjust the focus by setting the latitude and longitude
map = Basemap(projection='ortho', lat_0=20, lon_0=80)
# To shade terrain by relief. This step may take some time.
map.shadedrelief()
# Draw the country boundaries in white
map.drawcountries(color='white')
plt.show()
Here is how the plot looks:
Besides showing the earth as a sphere with orthogonal projection as shown in the preceding figure, we can also set projection='cyl' to use the Miller Cylindrical projection for a flat rectangular illustration.
Basemap provides plenty map drawing functions, such as drawing coastlines and plotting data over maps with hexbin or streamplot. Their details can be found in the official tutorials at http://basemaptutorial.readthedocs.io. As in-depth geographical analysis is beyond the scope of this book, we will leave a more specific exploration of its usage as an exercise for interested readers.