Last active
August 29, 2015 14:09
-
-
Save jhamman/452ca6a10068ec4399ee to your computer and use it in GitHub Desktop.
pcolor subplots using matplotlib.basemap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib as mpl | |
from matplotlib import cm | |
def cmap_discretize(cmap, N=10): | |
cmap = cm.get_cmap(eval(cmap)) | |
colors_i = np.concatenate((np.linspace(0, 1., N), (0.,0.,0.,0.))) | |
colors_rgba = cmap(colors_i) | |
indices = np.linspace(0, 1., N+1) | |
cdict = {} | |
for ki,key in enumerate(('red','green','blue')): | |
cdict[key] = [(indices[i], colors_rgba[i-1,ki], colors_rgba[i,ki]) for i in xrange(N+1)] | |
return mpl.colors.LinearSegmentedColormap(cmap.name + "_%d"%N, cdict, 1024) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib as mpl | |
import matplotlib.pyplot as plt | |
from matplotlib import cm | |
from mpl_toolkits.basemap import Basemap | |
import mpl_toolkits.basemap.cm as GMT | |
# import xray | |
import numpy as np | |
def sub_plot_pcolor(lons, lats, data, title=None, cmap=cm.jet, | |
vmin=None, vmax=None, cbar=True, cbar_location='bottom', | |
units=None): | |
if vmin is None: | |
vmin=data.min() | |
if vmax is None: | |
vmax=data.max() | |
projection = {'urcrnrlat': 27.511827255753555, | |
'urcrnrlon': 16.90845094934209, | |
'llcrnrlat': 16.534986367884521, | |
'llcrnrlon': 189.2229322311162, | |
'projection': 'lcc', | |
'rsphere': 6371200.0, | |
'lon_0': -114, | |
'lat_0': 90} | |
m = Basemap(**projection) | |
xi,yi = m(np.squeeze(lons),np.squeeze(lats)) | |
sp = m.pcolormesh(xi, yi, np.squeeze(data), vmin=vmin,vmax=vmax, cmap=cmap) | |
m.drawparallels(np.arange(-80.,81.,20.)) | |
m.drawmeridians(np.arange(-180.,181.,20.)) | |
m.drawcoastlines(color='k',linewidth=0.25); | |
if title: | |
plt.title(title,size=13) | |
if cbar: | |
cbar = m.colorbar(location=cbar_location) | |
cbar.set_label(units) | |
return sp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment