import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
The Basemap
class can be used in two ways. Calling a Basemap
object with a list of latitude/longitude coordinates, it converts them to projected x/y coordintes suitable for plotting. Second, the object provides methods for drawing coastlines, country borders, waterways and meridian lines.
In this example, I will plot a map of Germany in the UTM zone 32, the Universal Transverse Mercator coordinate system. Germany has an extent roughly from 45° N, 5° E to 55° N, 17° E, so make this the extent of the map. The central meridian for zone 32 is at 15° E.
bbox = (5, 46.5, 17, 55.5)
central_meridian = 15
The Basemap
constructor must be initialised with the desired map projection (tmerc
for Tranvsere Mercator), the map extent (llcrnrlon
, llcrnrlat
, urcrnrlon
, urcrnrlat
) and, in case of this projection, the projection center (lon_0
and lat_0
).
The argument resolution
determines the level of detail for the vector data that is loaded. Possible values are c
for crude (default), intermediate (i
), high (h
), and full (f
).
map = Basemap(projection='tmerc', resolution='i',
llcrnrlon=bbox[0], llcrnrlat=bbox[1],
urcrnrlon=bbox[2], urcrnrlat=bbox[3],
lat_0=0, lon_0=central_meridian)
places = [ # name, (lon, lat), label_align, label_offset
('Berlin', (13.383333, 52.516667), 'right', (-3,-8)),
('Cologne', (6.966667, 50.95), 'left', (3,3)),
('Frankfurt', (8.683333, 50.116667), 'left', (3,3)),
('Hamburg', (10.001389, 53.565278), 'left', (3,-12)),
('Munich', (11.566667, 48.133333), 'right', (-3,3))]
fig = plt.figure()
map.drawmapboundary(linewidth=0)
map.drawcountries(color=(.5,.5,.5), linewidth=.25)
map.drawcoastlines(color=(0.5,0.5,0.5), linewidth=.25)
for name, (lon, lat), label_align, label_offset in places:
map.scatter(lon, lat, 20, latlon=True, marker='.', color=(0,.4,.8))
(x, y) = map(lon, lat)
plt.annotate(
name, xy=(x,y), xytext=label_offset, color=(.2, .2, .2),
textcoords='offset points', ha=label_align, va='bottom')
for ext in ['png', 'pdf']:
plt.savefig('germany.{}'.format(ext), dpi=96, bbox_inches='tight')
from IPython.display import Image
Image(filename='germany.png')