def plot_tseries(dayrel, ind, std, clr, key, xlabel, ylabel): plt.plot(dayrel, ind, clr, label=key) plt.fill_between(dayrel, ind-std, ind+std, color=clr, alpha=0.2) plt.title(key, fontsize=12) plt.xlabel(xlabel) plt.ylabel(ylabel) plt.grid(True) plt.autoscale(tight=True) plt.figure(figsize=(9, 12)) nrow, ncol = 3, 2 clr = 'k' for i, key in enumerate(keys): ind = tseries_rel[key].mean(dim='year') std = tseries_rel[key].std(dim='year') row, col = atm.subplot_index(nrow, ncol, i + 1) plt.subplot(nrow, ncol, i + 1) if row == nrow: xlabel = 'Relative Day' else: xlabel = '' if col == 1: ylabel = 'Daily Index' else: ylabel = '' plot_tseries(dayrel, ind, std, clr, key, xlabel, ylabel) plt.suptitle('1979-2014 Climatological Composites Relative to Onset Day') saveclose('ind_tseries_composites', isave, exts)
def plot_tseries_together(data, onset=None, years=None, suptitle='', figsize=(14,10), legendsize=10, legendloc='lower right', nrow=3, ncol=4, yearnm='year', daynm='day', standardize=True, label_attr=None, data_style=None, onset_style=None, show_days=False): """Plot multiple daily timeseries together each year. Parameters ---------- data : xray.Dataset Dataset of timeseries variables to plot together. onset : ndarray or dict of ndarrays, optional Array of onset day for each year, or dict of onset arrays (e.g. to compare onset days from different methods). years : ndarray, optional Subset of years to include. If omitted, all years are included. suptitle : str, optional Supertitle for plot. figsize : 2-tuple, optional Size of each figure. legendsize : int, optional Font size for legend legendloc : str, optional Legend location nrow, ncol : int, optional Number of rows, columns in each figure. yearnm, daynm : str, optional Name of year and day dimensions in data. standardize : bool, optional If True, standardize each timeseries by dividing by its standard deviation. label_attr : str, optional Attribute of each data variable to use for labels. If omitted, then the variable name is used. data_style, onset_style : list or dict, optional Matlab-style strings for each data variable or onset index. show_days : bool, optional If True, annotate each subplot with a textbox showing the onset days. """ if years is None: # All years years = data[yearnm].values data = atm.subset(data, {yearnm : (years, None)}) if label_attr is not None: labels = {nm : data[nm].attrs[label_attr] for nm in data.data_vars} if onset is not None: if isinstance(onset, dict): if onset_style is None: onset_style = {key : 'k' for key in onset.keys()} else: onset = {'onset' : onset} if onset_style is None: onset_style = {'onset' : 'k'} textpos = {key : (0.05, 0.9 - 0.1*i) for i, key in enumerate(onset)} # Plot each year for y, year in enumerate(years): df = atm.subset(data, {yearnm : (year, None)}).to_dataframe() df.drop(yearnm, axis=1, inplace=True) if label_attr is not None: df.rename(columns=labels, inplace=True) if standardize: for key in df.columns: df[key] = (df[key] - np.nanmean(df[key])) / np.nanstd(df[key]) ylabel = 'Standardized Timeseries' else: ylabel = 'Timeseries' if y % (nrow * ncol) == 0: fig, axes = plt.subplots(nrow, ncol, figsize=figsize, sharex=True) plt.subplots_adjust(left=0.08, right=0.95, wspace=0.2, hspace=0.2) plt.suptitle(suptitle) yplot = 1 else: yplot += 1 i, j = atm.subplot_index(nrow, ncol, yplot) ax = axes[i-1, j-1] df.plot(ax=ax, style=data_style) ax.grid() if yplot == 1: ax.legend(fontsize=legendsize, loc=legendloc) else: ax.legend_.remove() if onset is not None: for key in onset: d0 = onset[key][y] ax.plot([d0, d0], ax.get_ylim(), onset_style[key]) if show_days: atm.text(d0, textpos[key], ax=ax, color=onset_style[key]) if j == 1: ax.set_ylabel(ylabel) if i == nrow: ax.set_xlabel('Day') else: ax.set_xlabel('') ax.set_title(year)
ylims = {"MFC": (-350, 400), "precip": (0, 1700)} for varnm in varnms: suptitle = "Onset/Retreat from Accum. %s Changepoints (Merged)" % varnm.upper() for y, year in enumerate(years): if y % (nrow * ncol) == 0: fig, axes = plt.subplots(nrow, ncol, figsize=figsize, sharex=True) plt.subplots_adjust(left=0.08, right=0.95, wspace=0.0, hspace=0.2) plt.suptitle(suptitle) yplot = 1 else: yplot += 1 plt.subplot(nrow, ncol, yplot) plotyear(chp[varnm], y, xlims, ylims[varnm]) row, col = atm.subplot_index(nrow, ncol, yplot) if row == nrow: plt.xlabel("Day") else: plt.gca().set_xticklabels("") if col > 1: plt.gca().set_yticklabels("") if isavefigs: atm.savefigs("onset_changepoint_merged_" + varnm.upper(), "pdf") plt.close("all") df = pd.DataFrame() for key in ["onset", "retreat"]: for varnm in varnms: df["%s_%s" % (key, varnm.upper())] = chp[varnm][key].to_series() atm.scatter_matrix(df, incl_p=True, incl_line=True, annotation_pos=(0.05, 0.7))