%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#this sets up the actual map
#http://matplotlib.org/basemap/users/mapsetup.html
m = Basemap(projection='cyl', lat_0 = 50, lon_0 = -100,
ax=ax, resolution = 'l', area_thresh = 1000,
llcrnrlat=-90,urcrnrlat=90, llcrnrlon=-180,urcrnrlon=180)
#m is your basemap object
#set up borders/boundaries
border_color = 'black'
m.drawcoastlines()
#http://matplotlib.org/basemap/users/geography.html?
<matplotlib.collections.LineCollection at 0xc979080>
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#this sets up the actual map
#http://matplotlib.org/basemap/users/mapsetup.html
m = Basemap(projection='cyl', lat_0 = 50, lon_0 = -100,
ax=ax, resolution = 'l', area_thresh = 1000,
llcrnrlat=-90,urcrnrlat=90, llcrnrlon=-180,urcrnrlon=180)
#m is your basemap object
#set up borders/boundaries
border_color = 'black'
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary()
#http://matplotlib.org/basemap/users/geography.html?
<matplotlib.patches.Rectangle at 0xc4c72b0>
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#this sets up the actual map
#http://matplotlib.org/basemap/users/mapsetup.html
m = Basemap(projection='cyl', lat_0 = 50, lon_0 = -100,
ax=ax, resolution = 'l', area_thresh = 1000,
llcrnrlat=-90,urcrnrlat=90, llcrnrlon=-180,urcrnrlon=180)
#m is your basemap object
#set up borders/boundaries
border_color = 'black'
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary()
#meridans
# meridians on bottom and left
parallels = np.arange(-90,90,30)
# labels = [left,right,top,bottom]
m.drawparallels(parallels)
meridians = np.arange(0,360,60)
m.drawmeridians(meridians)
#http://matplotlib.org/basemap/users/graticule.html
{-180: ([<matplotlib.lines.Line2D at 0xb20f8d0>], []), -120: ([<matplotlib.lines.Line2D at 0xb20fe10>], []), -60: ([<matplotlib.lines.Line2D at 0xcf3e358>], []), 0: ([<matplotlib.lines.Line2D at 0xcf3e860>], []), 60: ([<matplotlib.lines.Line2D at 0xcf3ed68>], []), 120: ([<matplotlib.lines.Line2D at 0xcf3f2b0>], []), 180: ([<matplotlib.lines.Line2D at 0xcf3f7b8>], [])}
"""
Example of adding points to the plot
http://www.packtpub.com/article/plotting-geographical-data-using-basemap
"""
# Cities names and coordinates
cities = ['London', 'New York', 'Madrid', 'Cairo',
'Moscow', 'Delhi', 'Dakar']
lat = [51.507778, 40.716667, 40.4, 30.058,
55.751667, 28.61, 14.692778]
lon = [-0.128056, -74, -3.683333, 31.229,
37.617778, 77.23, -17.446667]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
m = Basemap(projection='cyl', lat_0 = 50, lon_0 = -100,
ax=ax, resolution = 'l', area_thresh = 1000,
llcrnrlat=-90,urcrnrlat=90, llcrnrlon=-180,urcrnrlon=180)
border_color = 'black'
m.drawcoastlines(color=border_color)
m.drawcountries(color=border_color)
m.drawmapboundary(color=border_color)
# map city coordinates to map coordinates
x, y = m(lon, lat)
m.scatter(x,y, s=100, c='red', zorder=100)
<matplotlib.collections.PathCollection at 0xd4be860>
#change the projection
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
m = Basemap(projection='moll',lon_0=0,resolution='c',ax=ax)
border_color = '0.5'
m.drawcoastlines(color=border_color)
m.drawcountries(color=border_color)
m.drawmapboundary(color=border_color)
# map city coordinates to map coordinates
x, y = m(lon, lat)
m.scatter(x,y, s=100, c='red', zorder=100)
<matplotlib.collections.PathCollection at 0xb46f6a0>
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
m = Basemap(projection='moll',lon_0=0,resolution='c',ax=ax)
border_color = '0.5'
m.drawcoastlines(color=border_color)
m.drawcountries(color=border_color)
m.drawmapboundary(color=border_color)
# map city coordinates to map coordinates
x, y = m(lon, lat)
m.scatter(x,y, s=100, c='red', zorder=100)
for city, xc, yc in zip(cities, x, y):
#draw the city name in a yellow (shaded) box
ax.text(xc+5, yc, city, bbox=dict(facecolor='yellow', alpha=0.5))
import scipy.io.netcdf as ncd
fn="PRATE_197901_a.nc"
f= ncd.netcdf_file(fn)
print f.variables.keys()
data = np.squeeze(f.variables['PRATE_surface'][...])
lats = f.variables['latitude'][:]
lons = f.variables['longitude'][:]
print data.shape
['latitude', 'PRATE_surface', 'longitude', 'time'] (190L, 384L)
from mpl_toolkits.basemap import cm
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_title("PRATE 1979/01")
m = Basemap(projection='cyl',
ax=ax, resolution = 'l', area_thresh = 1000,
llcrnrlat=lats[0],urcrnrlat=lats[-1], llcrnrlon=lons[0],urcrnrlon=lons[-1])
border_color = 'k'
m.drawcoastlines(color=border_color)
m.drawcountries(color=border_color)
m.drawmapboundary(color=border_color)
m.drawmapboundary(fill_color='white')
#just use imshow/pcolor/pcolormesh/etc
im = m.imshow(data, cmap=cm.s3pcpn,interpolation='nearest')
#and always add a colorbar
fig.colorbar(im, ax=ax, fraction=.15,orientation='horizontal')
#http://matplotlib.org/basemap/users/examples.html
<matplotlib.colorbar.Colorbar instance at 0x000000000DDCE688>
What about shape files?
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
m = Basemap(projection='cyl', lat_0 = 50, lon_0 = -100,
ax=ax, resolution = 'l', area_thresh = 1000,
llcrnrlat=-90,urcrnrlat=90, llcrnrlon=-180,urcrnrlon=180)
border_color = '0.8'
m.drawcoastlines(color=border_color)
m.drawcountries(color=border_color)
m.drawmapboundary(color=border_color)
#sample file
shp_info = m.readshapefile('st99_d00', 'states', drawbounds=True)
ax = fig.gca() # get current axes instance
non_states = ['District of Columbia','Puerto Rico']
statenames = [sp['NAME'] for sp in m.states_info if sp['NAME']
not in non_states]
cls = ['r','g','b','y']
#add polygons to map
for nshape,seg in enumerate(m.states):
poly = Polygon(seg, ec='k', fc=cls[nshape%4])
ax.add_patch(poly)
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#we can change the bounding box
#http://www.mapsofworld.com/lat_long/usa-lat-long.html
upper=(50,-65)
lower=(25,-125)
m = Basemap(projection='cyl', ax=ax, resolution = 'l', area_thresh = 1000,
llcrnrlat=lower[0],urcrnrlat=upper[0],
llcrnrlon=lower[1],urcrnrlon=upper[1])
border_color = '0.8'
m.drawcoastlines(color=border_color)
m.drawcountries(color=border_color)
m.drawmapboundary(color=border_color)
#sample file
shp_info = m.readshapefile('st99_d00', 'states', drawbounds=True)
ax = fig.gca() # get current axes instance
non_states = ['District of Columbia','Puerto Rico']
statenames = [sp['NAME'] for sp in m.states_info if sp['NAME']
not in non_states]
cls = ['r','g','b','y']
#add polygons to map
for nshape,seg in enumerate(m.states):
poly = Polygon(seg, ec='k', fc=cls[nshape%4])
ax.add_patch(poly)