def _map(self, i): import maps log.debug('map of {}'.format([getattr(self, v)[i] for v in 'sxyzc'])) kwargs = self.opts(i) x, y, z = self.data(i) o = get_args_from(kwargs, margin = 0.05, width = 10e6, height = None, boundarylat = 50, projection = 'cyl', drawcoastline = 1, drawgrid = 1, drawspecgrid = 1, drawcountries = 0, bluemarble = 0, nightshade = None) m = maps.drawmap(y, x, **o) x, y = m(x, y) if z is None: l, = plt.plot(x, y, **kwargs) else: # linestyle must not be 'none' when plotting 3D if 'linestyle' in kwargs and kwargs['linestyle'] == 'none': kwargs['linestyle'] = ':' o = get_args_from(kwargs, markersize = 6, cbfrac = 0.04, cblabel = self.alabel('z')) p = set_defaults(kwargs, zorder = 100) l = plt.scatter(x, y, c = z, s = o.markersize ** 2, edgecolor = 'none', **p) m = 6.0 dmin, dmax = np.nanmin(z), np.nanmax(z) cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1) formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x)) cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter) cb.set_label(o.cblabel) self.legend.append((l, self.llabel(i)))
def _xy(self, i): log.debug('xy plot of {}'.format([getattr(self, v)[i] for v in 'sxyzc'])) kwargs = self.opts(i) x, y, z = self.data(i) if x is not None: args = (x, y) else: args = (y,) if z is None: l, = plt.plot(*args, **kwargs) else: # linestyle must not be 'none' when plotting 3D if 'linestyle' in kwargs and kwargs['linestyle'] == 'none': kwargs['linestyle'] = ':' o = get_args_from(kwargs, markersize = 2, cbfrac = 0.04, cblabel = self.alabel('z')) l = plt.scatter(x, y, c = z, s = o.markersize ** 2, edgecolor = 'none', **kwargs) m = 6.0 dmin, dmax = np.nanmin(z), np.nanmax(z) cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1) formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x)) cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter) cb.set_label(o.cblabel) self.legend.append((l, self.llabel(i))) # fit self.fit(i, x, y)
def drawmap(lat = None, lon = None, margin = 0.05, width = 1e6, height = None, boundarylat = 40, projection = 'cyl', drawcoastline = 1, drawcountries = 0, drawgrid = 1, drawspecgrid = 1, bluemarble = 0, nightshade = None, places = [('Neumayer-St.' , -70.6666 , -8.2666), ('Amundsen-Scott-St.' , -90. , 0.), ('DESY Zeuthen' , 52.346142 , 13.633432)]): sys.stdout = sys.__stderr__ minlat, maxlat = np.nanmin(lat), np.nanmax(lat) lat0 = (minlat + maxlat) / 2 minlat, maxlat = minlat - margin * (maxlat - minlat), maxlat + margin * (maxlat - minlat) minlat, maxlat = max(-90, minlat), min(90, maxlat) minlon, maxlon = np.nanmin(lon), np.nanmax(lon) lon0 = (minlon + maxlon) / 2 minlon, maxlon = minlon - margin * (maxlon - minlon), maxlon + margin * (maxlon - minlon) #minlon, maxlon = max(-90,minlon), min(90,maxlon) if projection in ('ortho', 'robin'): map_options = { 'lat_0' : lat0, 'lon_0' : lon0, 'resolution':'l'} latlabels = [1, 0, 0, 0] # left, right, top, bottom lonlabels = [0, 0, 0, 1] elif projection in ('npaeqd', 'nplaea', 'npstere', 'spaeqd', 'splaea', 'spstere'): # polar if projection.startswith('s'): boundarylat = -abs(boundarylat) else: boundarylat = abs(boundarylat) map_options = { 'lat_0' : lat0, 'lon_0' : lon0, 'boundinglat' :boundarylat, 'resolution':'l'} latlabels = [0, 0, 1, 1] lonlabels = [1, 1, 0, 0] elif projection in ('cyl', 'merc'): map_options = { 'llcrnrlon' : minlon, 'llcrnrlat' : minlat, 'urcrnrlon' : maxlon, 'urcrnrlat' : maxlat, 'resolution':'i'} latlabels = [1, 0, 0, 0] lonlabels = [0, 0, 0, 1] elif projection in ('aeqd', 'laea', 'stere'): if height is None: height = width / np.sqrt(2) map_options = { 'lat_0' : lat0, 'lon_0' : lon0, 'width':width, 'height':height , 'resolution':'i'} latlabels = [1, 0, 0, 0] lonlabels = [0, 0, 0, 1] else: raise ValueError('unknow projection: ' + projection) #print map_options m = bm.Basemap(projection = projection, **map_options) if bluemarble: m.bluemarble(scale = 0.5) #m.warpimage(image = 'c:/Program Files (x86)/Python27/Lib/site-packages/mpl_toolkits/basemap/data/etopo1.jpg', scale = 0.5) #m.warpimage(image = 'c:/Users/al/Downloads/bluemarble/Earthlights_2002-5400x2700.jpg', scale = 0.5) elif drawcoastline: m.drawcoastlines(color = 'k') if drawcountries: m.drawcountries(color = 'k') #m.drawrivers(color = 'b') if projection in ('ortho'): m.drawlsmask(land_color = '#f7d081', lakes = True) else: m.fillcontinents(color = '#f7d081') if nightshade is not None: if isinstance(nightshade, basestring): datetime = dp.parse(nightshade) m.nightshade(datetime, color = 'k', delta = 0.25, alpha = 0.7) if drawgrid: if projection in ('ortho', 'robin', 'npaeqd', 'nplaea', 'npstere', 'spaeqd', 'splaea', 'spstere'): parallels, meridians = np.arange(-90, 90, 10), np.arange(-180, 180, 20) else: parallels, meridians = ticks.get_ticks(m.llcrnrlat, m.urcrnrlat, 6), ticks.get_ticks(m.llcrnrlon, m.urcrnrlon, 6) pmc = '0.6' if bluemarble else 'k' m.drawparallels(parallels, labels = latlabels, linewidth = 0.5, color = pmc) m.drawmeridians(meridians, labels = lonlabels, linewidth = 0.5, color = pmc) if drawspecgrid: ecl = 23.44 # ecliptic m.drawparallels([-90 + ecl, -ecl, ecl, 90 - ecl], dashes = [2, 2], linewidth = 0.5, color = pmc) m.drawparallels([0], dashes = [4,4], linewidth = 0.5, color = pmc) m.drawmeridians([0], dashes = [4,4], linewidth = 0.5, color = pmc) m.drawmapboundary(linewidth = 1.5, fill_color = 'white') for name, la, lo in places: x, y = m(lo, la) m.plot(x, y, 'rs', markersize = 4) plt.annotate(name, (x, y), textcoords = 'offset points', xytext = (5, 2)) sys.stdout = sys.__stdout__ return m
def _hist2d(self, i): log.debug('2D histogram of {}'.format([getattr(self, v)[i] for v in 'sxyzc'])) kwargs = self.opts(i) x, y, z = self.data(i) o = get_args_from(kwargs, style = 'color', density = False, log = False, cbfrac = 0.04, cblabel = 'bincontent', levels = 10) filled = 'color' in o.style or ('fill' in o.style) o.update(get_args_from(kwargs, hidezero = o.log or filled, colorbar = filled, clabels = not filled)) # make binnings bins = self.bins(i, 'x') if bins == 0: bins = int(1 + np.log2(len(x))) xedges, xcenters, xwidths = get_binning(bins, x) bins = self.bins(i, 'y') if bins == 0: bins = int(1 + np.log2(len(y))) yedges, ycenters, ywidths = get_binning(bins, y) bincontents, _d1, _d2 = np.histogram2d(x, y, [xedges, yedges]) bincontents = np.transpose(bincontents) assert np.all(_d1 == xedges) assert np.all(_d2 == yedges) # statsbox self.stats_fields2d(i, bincontents, xcenters, ycenters) if o.density: bincontents = get_density2d(bincontents, xwidths, ywidths) if o.hidezero: bincontents[bincontents == 0] = np.nan if o.log: bincontents = np.log10(bincontents) formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(np.power(10, x))) else: formatter = mpl.ticker.FuncFormatter(func = lambda x, i:number_mathformat(x)) if 'color' in o.style: pargs = set_defaults(kwargs, cmap = 'jet', edgecolor = 'none') plt.pcolor(xedges, yedges, ma.array(bincontents, mask = np.isnan(bincontents)), **kwargs) elif 'box' in o.style: pargs = set_defaults(kwargs, color = (1, 1, 1, 0), marker = 's', edgecolor = 'k') n = bincontents.size s = bincontents.reshape(n) s = s / np.nanmax(s) * (72. / 2. * self.w / max(len(xcenters), len(ycenters))) ** 2 xcenters, ycenters = np.meshgrid(xcenters, ycenters) plt.scatter(xcenters.reshape(n), ycenters.reshape(n), s = s, **pargs) elif 'contour' in o.style: pargs = set_defaults(kwargs, cmap = 'jet') if not isinstance(pargs['cmap'], mpl.colors.Colormap): pargs['cmap'] = mpl.cm.get_cmap(pargs['cmap']) if filled: cs = plt.contourf(xcenters, ycenters, bincontents, o.levels, **pargs) else: cs = plt.contour(xcenters, ycenters, bincontents, o.levels, **pargs) if o.clabels: plt.clabel(cs, inline = 1) else: raise ValueError('unknown style ' + o.style) if o.colorbar: m = 6.0 dmin, dmax = np.nanmin(bincontents), np.nanmax(bincontents) if o.log: dmin, dmax = np.ceil(dmin), np.floor(dmax) + 1 step = max(1, np.floor((dmax - dmin) / m)) cticks = np.arange(dmin, dmax, step) else: cticks = ticks.get_ticks(dmin, dmax, m, only_inside = 1) cb = plt.colorbar(fraction = o.cbfrac, pad = 0.01, aspect = 40, ticks = cticks, format = formatter) cb.set_label(o.cblabel)
def drawmap(lat=None, lon=None, margin=0.05, width=1e6, height=None, boundarylat=40, projection='cyl', drawcoastline=1, drawcountries=0, drawgrid=1, drawspecgrid=1, bluemarble=0, nightshade=None, places=[('Neumayer-St.', -70.6666, -8.2666), ('Amundsen-Scott-St.', -90., 0.), ('DESY Zeuthen', 52.346142, 13.633432)]): sys.stdout = sys.__stderr__ minlat, maxlat = np.nanmin(lat), np.nanmax(lat) lat0 = (minlat + maxlat) / 2 minlat, maxlat = minlat - margin * (maxlat - minlat), maxlat + margin * ( maxlat - minlat) minlat, maxlat = max(-90, minlat), min(90, maxlat) minlon, maxlon = np.nanmin(lon), np.nanmax(lon) lon0 = (minlon + maxlon) / 2 minlon, maxlon = minlon - margin * (maxlon - minlon), maxlon + margin * ( maxlon - minlon) #minlon, maxlon = max(-90,minlon), min(90,maxlon) if projection in ('ortho', 'robin'): map_options = {'lat_0': lat0, 'lon_0': lon0, 'resolution': 'l'} latlabels = [1, 0, 0, 0] # left, right, top, bottom lonlabels = [0, 0, 0, 1] elif projection in ('npaeqd', 'nplaea', 'npstere', 'spaeqd', 'splaea', 'spstere'): # polar if projection.startswith('s'): boundarylat = -abs(boundarylat) else: boundarylat = abs(boundarylat) map_options = { 'lat_0': lat0, 'lon_0': lon0, 'boundinglat': boundarylat, 'resolution': 'l' } latlabels = [0, 0, 1, 1] lonlabels = [1, 1, 0, 0] elif projection in ('cyl', 'merc'): map_options = { 'llcrnrlon': minlon, 'llcrnrlat': minlat, 'urcrnrlon': maxlon, 'urcrnrlat': maxlat, 'resolution': 'i' } latlabels = [1, 0, 0, 0] lonlabels = [0, 0, 0, 1] elif projection in ('aeqd', 'laea', 'stere'): if height is None: height = width / np.sqrt(2) map_options = { 'lat_0': lat0, 'lon_0': lon0, 'width': width, 'height': height, 'resolution': 'i' } latlabels = [1, 0, 0, 0] lonlabels = [0, 0, 0, 1] else: raise ValueError('unknow projection: ' + projection) #print map_options m = bm.Basemap(projection=projection, **map_options) if bluemarble: m.bluemarble(scale=0.5) #m.warpimage(image = 'c:/Program Files (x86)/Python27/Lib/site-packages/mpl_toolkits/basemap/data/etopo1.jpg', scale = 0.5) #m.warpimage(image = 'c:/Users/al/Downloads/bluemarble/Earthlights_2002-5400x2700.jpg', scale = 0.5) elif drawcoastline: m.drawcoastlines(color='k') if drawcountries: m.drawcountries(color='k') #m.drawrivers(color = 'b') if projection in ('ortho'): m.drawlsmask(land_color='#f7d081', lakes=True) else: m.fillcontinents(color='#f7d081') if nightshade is not None: if isinstance(nightshade, basestring): datetime = dp.parse(nightshade) m.nightshade(datetime, color='k', delta=0.25, alpha=0.7) if drawgrid: if projection in ('ortho', 'robin', 'npaeqd', 'nplaea', 'npstere', 'spaeqd', 'splaea', 'spstere'): parallels, meridians = np.arange(-90, 90, 10), np.arange(-180, 180, 20) else: parallels, meridians = ticks.get_ticks(m.llcrnrlat, m.urcrnrlat, 6), ticks.get_ticks( m.llcrnrlon, m.urcrnrlon, 6) pmc = '0.6' if bluemarble else 'k' m.drawparallels(parallels, labels=latlabels, linewidth=0.5, color=pmc) m.drawmeridians(meridians, labels=lonlabels, linewidth=0.5, color=pmc) if drawspecgrid: ecl = 23.44 # ecliptic m.drawparallels([-90 + ecl, -ecl, ecl, 90 - ecl], dashes=[2, 2], linewidth=0.5, color=pmc) m.drawparallels([0], dashes=[4, 4], linewidth=0.5, color=pmc) m.drawmeridians([0], dashes=[4, 4], linewidth=0.5, color=pmc) m.drawmapboundary(linewidth=1.5, fill_color='white') for name, la, lo in places: x, y = m(lo, la) m.plot(x, y, 'rs', markersize=4) plt.annotate(name, (x, y), textcoords='offset points', xytext=(5, 2)) sys.stdout = sys.__stdout__ return m