def contourf_latday(var, is_precip=False, clev=None, cticks=None, climits=None, nc_pref=40, grp=None, xlims=(-120, 200), xticks=np.arange(-120, 201, 30), ylims=(-60, 60), yticks=np.arange(-60, 61, 20), dlist=None, grid=False, ind_nm='onset', xlabels=True): """Create a filled contour plot of data on latitude-day grid. """ var = atm.subset(var, {'lat': ylims}) vals = var.values.T lat = atm.get_coord(var, 'lat') days = atm.get_coord(var, 'dayrel') if var.min() < 0: symmetric = True else: symmetric = False if is_precip: cmap = 'PuBuGn' extend = 'max' else: cmap = 'RdBu_r' extend = 'both' if clev == None: cint = atm.cinterval(vals, n_pref=nc_pref, symmetric=symmetric) clev = atm.clevels(vals, cint, symmetric=symmetric) elif len(atm.makelist(clev)) == 1: if is_precip: clev = np.arange(0, 10 + clev / 2.0, clev) else: clev = atm.clevels(vals, clev, symmetric=symmetric) plt.contourf(days, lat, vals, clev, cmap=cmap, extend=extend) plt.colorbar(ticks=cticks) plt.clim(climits) atm.ax_lims_ticks(xlims, xticks, ylims, yticks) plt.grid(grid) if dlist is not None: for d0 in dlist: plt.axvline(d0, color='k') if xlabels: plt.gca().set_xticklabels(xticks) plt.xlabel('Days Since ' + ind_nm.capitalize()) else: plt.gca().set_xticklabels([]) if grp is not None and grp.col == 0: plt.ylabel('Latitude') return None
def plotyy(data1, data2=None, xname='dayrel', data1_styles=None, y2_opts={'color' : 'r', 'alpha' : 0.6, 'linewidth' : 2}, xlims=None, xticks=None, ylims=None, yticks=None, y2_lims=None, xlabel='', y1_label='', y2_label='', legend=False, legend_kw={'fontsize' : 9, 'handlelength' : 2.5}, x0_axvlines=None, grid=True): """Plot data1 and data2 together on different y-axes.""" data1, data2 = atm.to_dataset(data1), atm.to_dataset(data2) for nm in data1.data_vars: if data1_styles is None: plt.plot(data1[xname], data1[nm], label=nm) elif isinstance(data1_styles[nm], dict): plt.plot(data1[xname], data1[nm], label=nm, **data1_styles[nm]) else: plt.plot(data1[xname], data1[nm], data1_styles[nm], label=nm) atm.ax_lims_ticks(xlims, xticks, ylims, yticks) plt.grid(grid) if x0_axvlines is not None: for x0 in x0_axvlines: plt.axvline(x0, color='k') plt.xlabel(xlabel) plt.ylabel(y1_label) axes = [plt.gca()] if data2 is not None: plt.sca(plt.gca().twinx()) for nm in data2.data_vars: plt.plot(data2[xname], data2[nm], label=nm, **y2_opts) if y2_lims is not None: plt.ylim(y2_lims) if 'linewidth' in y2_opts: y2_opts.pop('linewidth') atm.fmt_axlabels('y', y2_label, **y2_opts) atm.ax_lims_ticks(xlims, xticks) axes = axes + [plt.gca()] if legend: if data2 is None: plt.legend(**legend_kw) else: atm.legend_2ax(axes[0], axes[1], **legend_kw) return axes
def contourf_latday(var, clev=None, title='', nc_pref=40, grp=None, xlims=(-120, 200), xticks=np.arange(-120, 201, 30), ylims=(-60, 60), yticks=np.arange(-60, 61, 20), dlist=None, grid=False): vals = var.values.T lat = atm.get_coord(var, 'lat') days = atm.get_coord(var, 'dayrel') if var.min() >= 0: cmap, extend, symmetric = 'PuBuGn', 'max', False else: cmap, extend, symmetric = 'RdBu_r', 'both', True if clev == None: cint = atm.cinterval(vals, n_pref=nc_pref, symmetric=symmetric) clev = atm.clevels(vals, cint, symmetric=symmetric) elif len(atm.makelist(clev)) == 1: if var.name == 'PREC': clev = np.arange(0, 10 + clev/2.0, clev) else: clev = atm.clevels(vals, clev, symmetric=symmetric) cticks_dict = {'PRECTOT' : np.arange(0, 13, 2), 'PREC' : np.arange(0, 11, 2), 'T200' : np.arange(-208, 227, 2), 'U200' : np.arange(-60, 61, 10), 'PSI500' : np.arange(-800, 801, 200)} cticks = cticks_dict.get(var.name) plt.contourf(days, lat, vals, clev, cmap=cmap, extend=extend) plt.colorbar(ticks=cticks) atm.ax_lims_ticks(xlims, xticks, ylims, yticks) plt.grid(grid) plt.title(title) if dlist is not None: for d0 in dlist: plt.axvline(d0, color='k') if grp is not None and grp.row == grp.ncol - 1: plt.xlabel('Rel Day') if grp is not None and grp.col == 0: plt.ylabel('Latitude')
def plot_index_years(index, nrow=3, ncol=4, fig_kw={'figsize' : (11, 7), 'sharex' : True, 'sharey' : True}, gridspec_kw={'left' : 0.1, 'right' : 0.95, 'wspace' : 0.05, 'hspace' : 0.1}, incl_fit=False, suptitle='', xlabel='Day', ylabel='Index', xlims=None, ylims=None, xticks=np.arange(0, 401, 100), grid=True): """Plot daily timeseries of monsoon onset/retreat index each year. """ years = atm.get_coord(index, 'year') days = atm.get_coord(index, 'day') grp = atm.FigGroup(nrow, ncol, fig_kw=fig_kw, gridspec_kw=gridspec_kw, suptitle=suptitle) for year in years: grp.next() ind = atm.subset(index, {'year' : (year, year)}, squeeze=True) ts = ind['tseries'] d0_list = [ind['onset'], ind['retreat']] plt.plot(days, ts, 'k') for d0 in d0_list: plt.axvline(d0, color='k') if incl_fit and 'tseries_fit_onset' in ind: plt.plot(days, ind['tseries_fit_onset'], 'r') if incl_fit and 'tseries_fit_retreat' in ind: plt.plot(days, ind['tseries_fit_retreat'], 'b') atm.text(year, (0.05, 0.9)) atm.ax_lims_ticks(xlims=xlims, ylims=ylims, xticks=xticks) plt.grid(grid) if grp.row == grp.nrow - 1: plt.xlabel(xlabel) if grp.col == 0: plt.ylabel(ylabel) return grp