Basemap

Let's see the world from the eyes of Python

First: import the libraries


In [ ]:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Now to use the Basemap function to create a map with a set projection define the centre point using lat/lon, resolution: l=low, h=high, and area_threshold: only plots coastal feastures greater than 1000km2


In [ ]:
my_map = Basemap(projection='ortho', lat_0=50, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()

Now to add a little bit of detail. Draw country boundaries, fill in the continents with a colour, and add bathymetry.


In [ ]:
my_map = Basemap(projection='ortho', lat_0=50, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.etopo()  # bathymetry

Having some gridlines always helps too! But let's drop the bathymetry for now.


In [ ]:
my_map = Basemap(projection='ortho', lat_0=50, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')

my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))

Want a different view? Try this...

Here we have set the central point to a latitude value of: 0


In [ ]:
my_map = Basemap(projection='ortho', lat_0=0, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))

Maybe we don't like this projection anymore. How about something else?


In [ ]:
my_map = Basemap(projection='robin', lat_0=0, lon_0=-20,
              resolution='l', area_thresh=1000.0)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))

# Find more here:
# http://matplotlib.org/basemap/users/mapsetup.html

Can you find the Isle of Wight?


In [ ]:
my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'l', area_thresh = 1000,
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()

my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
print("Apparently Python stole it for there supersnake cave.")

Maybe it is too big for our 'area threshold'

Tweak it down!

In [ ]:
my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'l', area_thresh = 0.1,
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
print("I always thought people from the Isle of Wight were a little bit square...")

Let's try and correct those squares by increasing the resolution.


In [ ]:
my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
print("That's a bit better!")

We'd like to know where Cowes is: the captial of the Isle of Wight


In [ ]:
my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))

lon = -1.3126
lat = 50.7507
x,y = my_map(lon, lat)
my_map.plot(x, y, 'bo', markersize=12)

How about Portsmouth and Southampton too?


In [ ]:
my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
lons = [-1.3126, -1.1010, -1.4043]
lats = [50.7507, 50.8429, 50.9096]
x,y = my_map(lons, lats)
my_map.plot(x, y, 'bo', markersize=10)

Labels always help


In [ ]:
my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
lons = [-1.3126, -1.1010, -1.4043]
lats = [50.7507, 50.8429, 50.9096]
x,y = my_map(lons, lats)
my_map.plot(x, y, 'bo', markersize=10)

labels = ['Cowes', 'Portsmouth', 'Southampton']
for label, xpt, ypt in zip(labels, x, y):
    plt.text(xpt, ypt, label)

Now to offset the labels slightly


In [ ]:
my_map = Basemap(projection='lcc', lat_0=50, lon_0=-20,
    resolution = 'i', area_thresh = 0.1, # i = intermediate resoltion, f = full resolution (might take a while!)
    llcrnrlon=-2, llcrnrlat=50.5,
    urcrnrlon=0, urcrnrlat=50.8)
 
my_map.drawcoastlines()
my_map.drawcountries()
my_map.fillcontinents(color='coral')
my_map.drawmapboundary()
 
my_map.drawmeridians(np.arange(0, 360, 30))
my_map.drawparallels(np.arange(-90, 90, 30))
 
lons = [-1.3126, -1.1010, -1.4043]
lats = [50.7507, 50.8429, 50.9096]
x,y = my_map(lons, lats)
my_map.plot(x, y, 'bo', markersize=10)

labels = ['Cowes', 'Portsmouth', 'Southampton']
for label, xpt, ypt in zip(labels, x, y):
    plt.text(xpt-7000, ypt+3000, label)

Can you create your own map and zoom into Norfolk, label Norwich, Lowestoft and Sherringham.


In [ ]:


In [ ]:
# source: http://introtopython.org/visualization_earthquakes.html