import numpy as np import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap # Import the Basemap toolkit import xarray as xr import sys import matplotlib.colors as colors file = sys.argv[1:] dset = xr.open_dataset(str(file[0])) lon_min = np.asscalar(np.min(dset['lon'])) lon_max = np.asscalar(np.max(dset['lon'])) lat_min = np.asscalar(np.min(dset['lat'])) lat_max = np.asscalar(np.max(dset['lat'])) print('Longitude min. %7.4f, max. %7.4f' % (lon_min, lon_max) ) print('Latitude min. %7.4f, max. %7.4f' % (lat_min,lat_max) ) def get_projection(dset): x, y = dset['lon'].values, dset['lat'].values m = Basemap(projection='mill', llcrnrlon=dset['lon'].min()-1., llcrnrlat=dset['lat'].min()-1., urcrnrlon=dset['lon'].max()+1., urcrnrlat=dset['lat'].max()+1., resolution='i') m.drawcountries(linewidth=0.5, color='black') m.shadedrelief() m.drawcoastlines() m.drawparallels(np.arange(-90.0, 90.0, 5.), linewidth=0.5, color='white', labels=[True, False, False, True], fontsize=7) m.drawmeridians(np.arange(0.0, 360.0, 5.), linewidth=0.5, color='white', labels=[True, False, False, True], fontsize=7) x, y = m(x, y) return(m, x, y) def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=256): """Truncate a colormap by specifying the start and endpoint.""" new_cmap = colors.LinearSegmentedColormap.from_list( 'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval), cmap(np.linspace(minval, maxval, n))) return(new_cmap) fig = plt.figure(figsize=(10,10)) m, x, y = get_projection(dset) cmap = truncate_colormap(plt.get_cmap('terrain'), 0.2, 1.) m.contourf(x, y, dset['topography_c'], tri=True, cmap=cmap, levels=np.arange(-100., 3000., 10.)) plt.show(block=True)