def lmplot(ax): n = 80 c = "#222222" rs = np.random.RandomState(5) x = rs.normal(4, 1, n) y = 2 + 1.5 * x + rs.normal(0, 3, n) ax.plot(x, y, "o", c=c, alpha=.8) xx = np.linspace(1 + 1e-9, 7 - 1e-9, 100) lmpred = lambda x, y: np.polyval(np.polyfit(x, y, 1), xx) yy = lmpred(x, y) ax.plot(xx, yy, c=c) boots = moss.bootstrap(x, y, func=lmpred, n_boot=100) ci = moss.percentiles(boots, [2.5, 97.5], 0) ax.fill_between(xx, *ci, alpha=.15, color=c) ax.set_title("lmplot()")
ax = plt.subplot(gs[:2, 0]) plt.title("lmplot()") n = 80 c = "#222222" rs = np.random.RandomState(5) x = rs.normal(4, 1, n) y = 2 + 1.5 * x + rs.normal(0, 3, n) ax.plot(x, y, "o", c=c, alpha=.8) xx = np.linspace(1, 7, 100) lmpred = lambda x, y: np.polyval(np.polyfit(x, y, 1), xx) yy = lmpred(x, y) ax.plot(xx, yy, c=c) boots = moss.bootstrap(x, y, func=lmpred, n_boot=100) ci = moss.percentiles(boots, [2.5, 97.5], 0) ax.fill_between(xx, *ci, alpha=.15, color=c) # Timeseries plot # --------------- ax = plt.subplot(gs[:2, 1]) plt.title("tsplot()") n = 20 t = 10 ax.set_xlim(0, t) x = np.linspace(0, t, 100) s = np.array([stats.gamma.pdf(x, a) for a in [3, 5, 7]]) d = s[:, np.newaxis, :]
def regplot(x, y, data=None, corr_func=stats.pearsonr, xlabel="", ylabel="", ci=95, size=None, annotloc=None, color=None, reg_kws=None, scatter_kws=None, dist_kws=None, text_kws=None): """Scatterplot with regreesion line, marginals, and correlation value. Parameters ---------- x : sequence independent variables y : sequence dependent variables data : dataframe, optional if dataframe is given, x, and y are interpreted as string keys mapping to dataframe column names corr_func : callable, optional correlation function; expected to take two arrays and return a (statistic, pval) tuple xlabel, ylabel : string, optional label names ci : int or None confidence interval for the regression line size: int figure size (will be a square; only need one int) annotloc : two or three tuple (xpos, ypos [, horizontalalignment]) color : matplotlib color scheme color of everything but the regression line overridden by passing `color` to subfunc kwargs {reg, scatter, dist, text}_kws: dicts further keyword arguments for the constituent plots """ # Interperet inputs if data is not None: xlabel, ylabel = x, y x = np.array(data[x]) y = np.array(data[y]) # Set up the figure and axes size = 6 if size is None else size fig = plt.figure(figsize=(size, size)) ax_scatter = fig.add_axes([0.05, 0.05, 0.75, 0.75]) ax_x_marg = fig.add_axes([0.05, 0.82, 0.75, 0.13]) ax_y_marg = fig.add_axes([0.82, 0.05, 0.13, 0.75]) # Plot the scatter if scatter_kws is None: scatter_kws = {} if color is not None and "color" not in scatter_kws: scatter_kws.update(color=color) marker = scatter_kws.pop("markerstyle", "o") alpha_maker = stats.norm(0, 100) alpha = alpha_maker.pdf(len(x)) / alpha_maker.pdf(0) alpha = max(alpha, .1) alpha = scatter_kws.pop("alpha", alpha) ax_scatter.plot(x, y, marker, alpha=alpha, mew=0, **scatter_kws) ax_scatter.set_xlabel(xlabel) ax_scatter.set_ylabel(ylabel) # Marginal plots using our distplot function if dist_kws is None: dist_kws = {} if color is not None and "color" not in dist_kws: dist_kws.update(color=color) if "legend" not in dist_kws: dist_kws["legend"] = False distplot(x, ax=ax_x_marg, **dist_kws) distplot(y, ax=ax_y_marg, vertical=True, **dist_kws) for ax in [ax_x_marg, ax_y_marg]: ax.set_xticklabels([]) ax.set_yticklabels([]) # Regression line plot xlim = ax_scatter.get_xlim() a, b = np.polyfit(x, y, 1) if reg_kws is None: reg_kws = {} reg_color = reg_kws.pop("color", "#222222") ax_scatter.plot(xlim, np.polyval([a, b], xlim), color=reg_color, **reg_kws) # Bootstrapped regression standard error if ci is not None: xx = np.linspace(xlim[0], xlim[1], 100) def _bootstrap_reg(x, y): fit = np.polyfit(x, y, 1) return np.polyval(fit, xx) boots = moss.bootstrap(x, y, func=_bootstrap_reg) ci_lims = [50 - ci / 2., 50 + ci / 2.] ci_band = moss.percentiles(boots, ci_lims, axis=0) ax_scatter.fill_between(xx, *ci_band, color=reg_color, alpha=.15) ax_scatter.set_xlim(xlim) # Calcluate a correlation statistic and p value r, p = corr_func(x, y) msg = "%s: %.3f (p=%.3g%s)" % (corr_func.__name__, r, p, moss.sig_stars(p)) if annotloc is None: xmin, xmax = xlim x_range = xmax - xmin if r < 0: xloc, align = xmax - x_range * .02, "right" else: xloc, align = xmin + x_range * .02, "left" ymin, ymax = ax_scatter.get_ylim() y_range = ymax - ymin yloc = ymax - y_range * .02 else: if len(annotloc) == 3: xloc, yloc, align = annotloc else: xloc, yloc = annotloc align = "left" if text_kws is None: text_kws = {} ax_scatter.text(xloc, yloc, msg, ha=align, va="top", **text_kws) # Set the axes on the marginal plots ax_x_marg.set_xlim(ax_scatter.get_xlim()) ax_x_marg.set_yticks([]) ax_y_marg.set_ylim(ax_scatter.get_ylim()) ax_y_marg.set_xticks([])
def lmplot(x, y, data, color=None, row=None, col=None, x_estimator=None, x_ci=95, fit_line=True, ci=95, truncate=False, sharex=True, sharey=True, palette="hls", size=None, scatter_kws=None, line_kws=None, palette_kws=None): """Plot a linear model from a DataFrame. Parameters ---------- x, y : strings column names in `data` DataFrame for x and y variables data : DataFrame source of data for the model color : string, optional DataFrame column name to group the model by color row, col : strings, optional DataFrame column names to make separate plot facets x_estimator : callable, optional Interpret X values as factor labels and use this function to plot the point estimate and bootstrapped CI x_ci : int optional size of confidence interval for x_estimator error bars fit_line : bool, optional if True fit a regression line by color/row/col and plot ci : int, optional confidence interval for the regression line truncate : bool, optional if True, only fit line from data min to data max sharex, sharey : bools, optional only relevant if faceting; passed to plt.subplots palette : seaborn color palette argument if using separate plots by color, draw with this color palette size : float, optional size (plots are square) for each plot facet {scatter, line}_kws : dictionary keyword arguments to pass to the underlying plot functions palette_kws : dictionary keyword arguments for seaborn.color_palette """ # TODO # - position_{dodge, jitter} # - legend when fit_line is False # - truncate fit # - wrap title when wide # - wrap columns # First sort out the general figure layout if size is None: size = mpl.rcParams["figure.figsize"][1] nrow = 1 if row is None else len(data[row].unique()) ncol = 1 if col is None else len(data[col].unique()) f, axes = plt.subplots(nrow, ncol, sharex=sharex, sharey=sharey, figsize=(size * ncol, size * nrow)) axes = np.atleast_2d(axes).reshape(nrow, ncol) if nrow == 1: row_masks = [np.repeat(True, len(data))] else: row_vals = np.sort(data[row].unique()) row_masks = [data[row] == val for val in row_vals] if ncol == 1: col_masks = [np.repeat(True, len(data))] else: col_vals = np.sort(data[col].unique()) col_masks = [data[col] == val for val in col_vals] if palette_kws is None: palette_kws = {} # Sort out the plot colors color_factor = color if color is None: hue_masks = [np.repeat(True, len(data))] colors = ["#222222"] else: hue_vals = np.sort(data[color].unique()) hue_masks = [data[color] == val for val in hue_vals] colors = color_palette(palette, len(hue_masks), **palette_kws) # Default keyword arguments for plot components if scatter_kws is None: scatter_kws = {} if line_kws is None: line_kws = {} # First walk through the facets and plot the scatters for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): ax = axes[row_i, col_j] if not sharex or (row_i + 1 == len(row_masks)): ax.set_xlabel(x) if not sharey or col_j == 0: ax.set_ylabel(y) # Title the plot if we are faceting title = "" if row is not None: title += "%s = %s" % (row, row_vals[row_i]) if row is not None and col is not None: title += " | " if col is not None: title += "%s = %s" % (col, col_vals[col_j]) ax.set_title(title) for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] if x_estimator is not None: ms = scatter_kws.pop("ms", 7) mew = scatter_kws.pop("mew", 0) x_vals = data_ijk[x].unique() y_grouped = [ np.array(data_ijk[y][data_ijk[x] == v]) for v in x_vals ] y_est = [x_estimator(y_i) for y_i in y_grouped] y_boots = [ moss.bootstrap(np.array(y_i), func=x_estimator) for y_i in y_grouped ] ci_lims = [50 - x_ci / 2., 50 + x_ci / 2.] y_ci = [moss.percentiles(y_i, ci_lims) for y_i in y_boots] y_error = ci_to_errsize(np.transpose(y_ci), y_est) ax.plot(x_vals, y_est, "o", mew=mew, ms=ms, color=color, **scatter_kws) ax.errorbar(x_vals, y_est, y_error, fmt=None, ecolor=color) else: ms = scatter_kws.pop("ms", 4) mew = scatter_kws.pop("mew", 0) ax.plot(data_ijk[x], data_ijk[y], "o", color=color, mew=mew, ms=ms, **scatter_kws) for ax_i in np.ravel(axes): ax_i.set_xmargin(.05) ax_i.autoscale_view() # Now walk through again and plot the regression estimate # and a confidence interval for the regression line if fit_line: for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): ax = axes[row_i, col_j] xlim = ax.get_xlim() for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] x_vals = np.array(data_ijk[x]) y_vals = np.array(data_ijk[y]) # Sort out the limit of the fit if truncate: xx = np.linspace(x_vals.min(), x_vals.max(), 100) else: xx = np.linspace(xlim[0], xlim[1], 100) # Inner function to bootstrap the regression def _bootstrap_reg(x, y): fit = np.polyfit(x, y, 1) return np.polyval(fit, xx) # Regression line confidence interval if ci is not None: ci_lims = [50 - ci / 2., 50 + ci / 2.] boots = moss.bootstrap(x_vals, y_vals, func=_bootstrap_reg) ci_band = moss.percentiles(boots, ci_lims, axis=0) ax.fill_between(xx, *ci_band, color=color, alpha=.15) fit = np.polyfit(x_vals, y_vals, 1) reg = np.polyval(fit, xx) if color_factor is None: label = "" else: label = hue_vals[hue_k] ax.plot(xx, reg, color=color, label=str(label), **line_kws) ax.set_xlim(xlim) # Plot the legend on the upper left facet and adjust the layout if color_factor is not None: axes[0, 0].legend(loc="best", title=color_factor) plt.tight_layout()
def tsplot(x, data, err_style=["ci_band"], ci=68, interpolate=True, estimator=np.mean, n_boot=10000, smooth=False, err_palette=None, ax=None, **kwargs): """Plot timeseries from a set of observations. Parameters ---------- x : n_tp array x values data : n_obs x n_tp array array of timeseries data where first axis is e.g. subjects err_style : list of strings names of ways to plot uncertainty across observations from set of {ci_band, ci_bars, boot_traces, book_kde, obs_traces, obs_points} ci : int or list of ints confidence interaval size(s). if a list, it will stack the error plots for each confidence interval estimator : callable function to determine centralt tendency and to pass to bootstrap must take an ``axis`` argument n_boot : int number of bootstrap iterations smooth : boolean whether to perform a smooth bootstrap (resample from KDE) ax : axis object, optional plot in given axis; if None creates a new figure kwargs : further keyword arguments for main call to plot() Returns ------- ax : matplotlib axis axis with plot data """ if ax is None: ax = plt.subplot(111) # Bootstrap the data for confidence intervals boot_data = moss.bootstrap(data, n_boot=n_boot, smooth=smooth, axis=0, func=estimator) ci_list = hasattr(ci, "__iter__") if not ci_list: ci = [ci] ci_vals = [(50 - w / 2, 50 + w / 2) for w in ci] cis = [moss.percentiles(boot_data, ci, axis=0) for ci in ci_vals] central_data = estimator(data, axis=0) # Plot the timeseries line to get its color line, = ax.plot(x, central_data, **kwargs) color = line.get_color() line.remove() kwargs.pop("color", None) # Use subroutines to plot the uncertainty for style in err_style: # Grab the function from the global environment try: plot_func = globals()["_plot_%s" % style] except KeyError: raise ValueError("%s is not a valid err_style" % style) # Possibly set up to plot each observation in a different color if err_palette is not None and "obs" in style: orig_color = color color = color_palette(err_palette, len(data), desat=.99) plot_kwargs = dict(ax=ax, x=x, data=data, boot_data=boot_data, central_data=central_data, color=color) for ci_i in cis: plot_kwargs["ci"] = ci_i plot_func(**plot_kwargs) if err_palette is not None and "obs" in style: color = orig_color # Replot the central trace so it is prominent marker = kwargs.pop("marker", "" if interpolate else "o") linestyle = kwargs.pop("linestyle", "-" if interpolate else "") ax.plot(x, central_data, color=color, marker=marker, linestyle=linestyle, **kwargs) return ax
def regplot(x, y, data=None, corr_func=stats.pearsonr, func_name=None, xlabel="", ylabel="", ci=95, size=None, annotloc=None, color=None, reg_kws=None, scatter_kws=None, dist_kws=None, text_kws=None): """Scatterplot with regresion line, marginals, and correlation value. Parameters ---------- x : sequence or string Independent variable. y : sequence or string Dependent variable. data : dataframe, optional If dataframe is given, x, and y are interpreted as string keys for selecting to dataframe column names. corr_func : callable, optional Correlation function; expected to take two arrays and return a numeric or (statistic, pval) tuple. func_name : string, optional Use in lieu of function name for fit statistic annotation. xlabel, ylabel : string, optional Axis label names if inputs are not Pandas objects or to override. ci : int or None Confidence interval for the regression estimate. size: int Figure size (will be a square; only need one int). annotloc : two or three tuple Specified with (xpos, ypos [, horizontalalignment]). color : matplotlib color scheme Color of everything but the regression line; can be overridden by passing `color` to subfunc kwargs. {reg, scatter, dist, text}_kws: dicts Further keyword arguments for the constituent plots. """ # Interperet inputs if data is not None: if not xlabel: xlabel = x if not ylabel: ylabel = y x = data[x].values y = data[y].values else: if hasattr(x, "name") and not xlabel: if x.name is not None: xlabel = x.name if hasattr(y, "name") and not ylabel: if y.name is not None: ylabel = y.name x = np.asarray(x) y = np.asarray(y) # Set up the figure and axes size = mpl.rcParams["figure.figsize"][0] if size is None else size fig = plt.figure(figsize=(size, size)) ax_scatter = fig.add_axes([0.05, 0.05, 0.75, 0.75]) ax_x_marg = fig.add_axes([0.05, 0.82, 0.75, 0.13]) ax_y_marg = fig.add_axes([0.82, 0.05, 0.13, 0.75]) # Plot the scatter if scatter_kws is None: scatter_kws = {} if color is not None and "color" not in scatter_kws: scatter_kws.update(color=color) marker = scatter_kws.pop("markerstyle", "o") alpha_maker = stats.norm(0, 100) alpha = alpha_maker.pdf(len(x)) / alpha_maker.pdf(0) alpha = max(alpha, .1) alpha = scatter_kws.pop("alpha", alpha) ax_scatter.plot(x, y, marker, alpha=alpha, mew=0, **scatter_kws) ax_scatter.set_xlabel(xlabel) ax_scatter.set_ylabel(ylabel) # Marginal plots using our distplot function if dist_kws is None: dist_kws = {} if color is not None and "color" not in dist_kws: dist_kws.update(color=color) dist_kws["xlabel"] = False distplot(x, ax=ax_x_marg, **dist_kws) distplot(y, ax=ax_y_marg, vertical=True, **dist_kws) for ax in [ax_x_marg, ax_y_marg]: ax.set_xticklabels([]) ax.set_yticklabels([]) # Regression line plot xlim = ax_scatter.get_xlim() a, b = np.polyfit(x, y, 1) if reg_kws is None: reg_kws = {} reg_color = reg_kws.pop("color", "#222222") ax_scatter.plot(xlim, np.polyval([a, b], xlim), color=reg_color, **reg_kws) # Bootstrapped regression standard error if ci is not None: xx = np.linspace(xlim[0], xlim[1], 100) def _bootstrap_reg(x, y): fit = np.polyfit(x, y, 1) return np.polyval(fit, xx) boots = moss.bootstrap(x, y, func=_bootstrap_reg) ci_lims = [50 - ci / 2., 50 + ci / 2.] ci_band = moss.percentiles(boots, ci_lims, axis=0) ax_scatter.fill_between(xx, *ci_band, color=reg_color, alpha=.15) ax_scatter.set_xlim(xlim) # Calcluate a fit statistic and p value if func_name is None: func_name = corr_func.__name__ out = corr_func(x, y) try: s, p = out msg = "%s: %.3f (p=%.3g%s)" % (func_name, s, p, moss.sig_stars(p)) except TypeError: s = corr_func(x, y) msg = "%s: %.3f" % (func_name, s) if annotloc is None: xmin, xmax = xlim x_range = xmax - xmin # Assume the fit statistic is correlation-esque for some # intuition on where the fit annotation should go if s < 0: xloc, align = xmax - x_range * .02, "right" else: xloc, align = xmin + x_range * .02, "left" ymin, ymax = ax_scatter.get_ylim() y_range = ymax - ymin yloc = ymax - y_range * .02 else: if len(annotloc) == 3: xloc, yloc, align = annotloc else: xloc, yloc = annotloc align = "left" if text_kws is None: text_kws = {} ax_scatter.text(xloc, yloc, msg, ha=align, va="top", **text_kws) # Set the axes on the marginal plots ax_x_marg.set_xlim(ax_scatter.get_xlim()) ax_x_marg.set_yticks([]) ax_y_marg.set_ylim(ax_scatter.get_ylim()) ax_y_marg.set_xticks([])
def lmplot(x, y, data, color=None, row=None, col=None, col_wrap=None, x_estimator=None, x_ci=95, n_boot=5000, fit_reg=True, order=1, ci=95, logistic=False, truncate=False, x_partial=None, y_partial=None, x_jitter=None, y_jitter=None, sharex=True, sharey=True, palette="husl", size=None, scatter_kws=None, line_kws=None, palette_kws=None): """Plot a linear model from a DataFrame. Parameters ---------- x, y : strings column names in `data` DataFrame for x and y variables data : DataFrame source of data for the model color : string, optional DataFrame column name to group the model by color row, col : strings, optional DataFrame column names to make separate plot facets col_wrap : int, optional wrap col variable at this width - cannot be used with row facet x_estimator : callable, optional Interpret X values as factor labels and use this function to plot the point estimate and bootstrapped CI x_ci : int optional size of confidence interval for x_estimator error bars n_boot : int, optional number of bootstrap iterations to perform fit_reg : bool, optional if True fit a regression model by color/row/col and plot order : int, optional order of the regression polynomial to fit (default = 1) ci : int, optional confidence interval for the regression line logistic : bool, optional fit the regression line with logistic regression truncate : bool, optional if True, only fit line from data min to data max {x, y}_partial : string or list of strings, optional regress these variables out of the factors before plotting {x, y}_jitter : float, optional parameters for uniformly distributed random noise added to positions sharex, sharey : bools, optional only relevant if faceting; passed to plt.subplots palette : seaborn color palette argument if using separate plots by color, draw with this color palette size : float, optional size (plots are square) for each plot facet {scatter, line}_kws : dictionary keyword arguments to pass to the underlying plot functions palette_kws : dictionary keyword arguments for seaborn.color_palette """ # TODO # - legend when fit_line is False # - wrap title when wide # First sort out the general figure layout if size is None: size = mpl.rcParams["figure.figsize"][1] if col is None and col_wrap is not None: raise ValueError("Need column facet variable for `col_wrap`") if row is not None and col_wrap is not None: raise ValueError("Cannot facet rows when using `col_wrap`") nrow = 1 if row is None else len(data[row].unique()) ncol = 1 if col is None else len(data[col].unique()) if col_wrap is not None: ncol = col_wrap nrow = int(np.ceil(len(data[col].unique()) / col_wrap)) f, axes = plt.subplots(nrow, ncol, sharex=sharex, sharey=sharey, figsize=(size * ncol, size * nrow)) axes = np.atleast_2d(axes).reshape(nrow, ncol) if nrow == 1 or col_wrap is not None: row_masks = [np.repeat(True, len(data))] else: row_vals = np.sort(data[row].unique()) row_masks = [data[row] == val for val in row_vals] if ncol == 1: col_masks = [np.repeat(True, len(data))] else: col_vals = np.sort(data[col].unique()) col_masks = [data[col] == val for val in col_vals] if x_partial is not None: if not isinstance(x_partial, list): x_partial = [x_partial] if y_partial is not None: if not isinstance(y_partial, list): y_partial = [y_partial] if palette_kws is None: palette_kws = {} # Sort out the plot colors color_factor = color if color is None: hue_masks = [np.repeat(True, len(data))] colors = ["#222222"] else: hue_vals = np.sort(data[color].unique()) hue_masks = [data[color] == val for val in hue_vals] colors = color_palette(palette, len(hue_masks), **palette_kws) # Default keyword arguments for plot components if scatter_kws is None: scatter_kws = {} if line_kws is None: line_kws = {} # First walk through the facets and plot the scatters scatter_ms = scatter_kws.pop("ms", 4) scatter_mew = mew = scatter_kws.pop("mew", 0) scatter_alpha = mew = scatter_kws.pop("alpha", .77) for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): if col_wrap is not None: f_row = col_j // ncol f_col = col_j % ncol else: f_row, f_col = row_i, col_j ax = axes[f_row, f_col] if f_row + 1 == nrow: ax.set_xlabel(x) if f_col == 0: ax.set_ylabel(y) # Title the plot if we are faceting title = "" if row is not None: title += "%s = %s" % (row, row_vals[row_i]) if row is not None and col is not None: title += " | " if col is not None: title += "%s = %s" % (col, col_vals[col_j]) ax.set_title(title) for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] if x_estimator is not None: ms = scatter_kws.pop("ms", 7) mew = scatter_kws.pop("mew", 0) x_vals = data_ijk[x].unique() y_vals = data_ijk[y] if y_partial is not None: for var in y_partial: conf = data_ijk[var] conf -= conf.mean() y_mean = y_vals.mean() y_vals = moss.vector_reject(y_vals - y_mean, conf) y_vals += y_mean y_grouped = [np.array(y_vals[data_ijk[x] == v]) for v in x_vals] y_est = [x_estimator(y_i) for y_i in y_grouped] y_boots = [moss.bootstrap(np.array(y_i), func=x_estimator, n_boot=n_boot) for y_i in y_grouped] ci_lims = [50 - x_ci / 2., 50 + x_ci / 2.] y_ci = [moss.percentiles(y_i, ci_lims) for y_i in y_boots] y_error = ci_to_errsize(np.transpose(y_ci), y_est) ax.plot(x_vals, y_est, "o", mew=mew, ms=ms, color=color, **scatter_kws) ax.errorbar(x_vals, y_est, y_error, fmt=None, ecolor=color) else: x_ = data_ijk[x] y_ = data_ijk[y] if x_partial is not None: for var in x_partial: conf = data_ijk[var] conf -= conf.mean() x_mean = x_.mean() x_ = moss.vector_reject(x_ - x_mean, conf) x_ += x_mean if y_partial is not None: for var in y_partial: conf = data_ijk[var] conf -= conf.mean() y_mean = y_.mean() y_ = moss.vector_reject(y_ - y_mean, conf) y_ += y_mean if x_jitter is not None: x_ += np.random.uniform(-x_jitter, x_jitter, x_.shape) if y_jitter is not None: y_ += np.random.uniform(-y_jitter, y_jitter, y_.shape) ax.plot(x_, y_, "o", color=color, alpha=scatter_alpha, mew=scatter_mew, ms=scatter_ms, **scatter_kws) for ax_i in np.ravel(axes): ax_i.set_xmargin(.05) ax_i.autoscale_view() # Now walk through again and plot the regression estimate # and a confidence interval for the regression line if fit_reg: for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): if col_wrap is not None: f_row = col_j // ncol f_col = col_j % ncol else: f_row, f_col = row_i, col_j ax = axes[f_row, f_col] xlim = ax.get_xlim() for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] x_vals = np.array(data_ijk[x]) y_vals = np.array(data_ijk[y]) if not len(x_vals): continue # Sort out the limit of the fit if truncate: xx = np.linspace(x_vals.min(), x_vals.max(), 100) else: xx = np.linspace(xlim[0], xlim[1], 100) xx_ = sm.add_constant(xx, prepend=True) # Inner function to bootstrap the regression def _regress(x, y): if logistic: x_ = sm.add_constant(x, prepend=True) fit = sm.GLM(y, x_, family=sm.families.Binomial()).fit() reg = fit.predict(xx_) else: fit = np.polyfit(x, y, order) reg = np.polyval(fit, xx) return reg # Remove nuisance variables with vector rejection if x_partial is not None: for var in x_partial: conf = data_ijk[var] conf -= conf.mean() x_mean = x_vals.mean() x_vals = moss.vector_reject(x_vals - x_mean, conf) x_vals += x_mean if y_partial is not None: for var in y_partial: conf = data_ijk[var] conf -= conf.mean() y_mean = y_vals.mean() y_vals = moss.vector_reject(y_vals - y_mean, conf) y_vals += y_mean # Regression line confidence interval if ci is not None: ci_lims = [50 - ci / 2., 50 + ci / 2.] boots = moss.bootstrap(x_vals, y_vals, func=_regress, n_boot=n_boot) ci_band = moss.percentiles(boots, ci_lims, axis=0) ax.fill_between(xx, *ci_band, color=color, alpha=.15) # Regression line reg = _regress(x_vals, y_vals) if color_factor is None: label = "" else: label = hue_vals[hue_k] ax.plot(xx, reg, color=color, label=str(label), **line_kws) ax.set_xlim(xlim) # Plot the legend on the upper left facet and adjust the layout if color_factor is not None and color_factor not in [row, col]: axes[0, 0].legend(loc="best", title=color_factor) plt.tight_layout()
def tsplot(data, time=None, unit=None, condition=None, value=None, err_style="ci_band", ci=68, interpolate=True, color=None, estimator=np.mean, n_boot=5000, err_palette=None, err_kws=None, legend=True, ax=None, **kwargs): """Plot one or more timeseries with flexible representation of uncertainty. This function can take data specified either as a long-form (tidy) DataFrame or as an ndarray with dimensions for sampling unit, time, and (optionally) condition. The interpretation of some of the other parameters changes depending on the type of object passed as data. Parameters ---------- data : DataFrame or ndarray Data for the plot. Should either be a "long form" dataframe or an array with dimensions (unit, time, condition). In both cases, the condition field/dimension is optional. The type of this argument determines the interpretation of the next few parameters. time : string or series-like Either the name of the field corresponding to time in the data DataFrame or x values for a plot when data is an array. If a Series, the name will be used to label the x axis. value : string Either the name of the field corresponding to the data values in the data DataFrame (i.e. the y coordinate) or a string that forms the y axis label when data is an array. unit : string Field in the data DataFrame identifying the sampling unit (e.g. subject, neuron, etc.). The error representation will collapse over units at each time/condition observation. This has no role when data is an array. condition : string or Series-like Either the name of the field identifying the condition an observation falls under in the data DataFrame, or a sequence of names with a length equal to the size of the third dimension of data. There will be a separate trace plotted for each condition. If condition is a Series with a name attribute, the name will form the title for the plot legend (unless legend is set to False). err_style : string or list of strings or None Names of ways to plot uncertainty across units from set of {ci_band, ci_bars, boot_traces, book_kde, unit_traces, unit_points}. Can use one or more than one method. ci : float or list of floats in [0, 100] Confidence interaval size(s). If a list, it will stack the error plots for each confidence interval. Only relevant for error styles with "ci" in the name. interpolate : boolean Whether to do a linear interpolation between each timepoint when plotting. The value of this parameter also determines the marker used for the main plot traces, unless marker is specified as a keyword argument. color : seaborn palette or matplotlib color name or dictionary Palette or color for the main plots and error representation (unless plotting by unit, which can be separately controlled with err_palette). If a dictionary, should map condition name to color spec. estimator : callable Function to determine central tendency and to pass to bootstrap must take an ``axis`` argument. n_boot : int Number of bootstrap iterations. err_palette: seaborn palette Palette name or list of colors used when plotting data for each unit. err_kws : dict, optional Keyword argument dictionary passed through to matplotlib function generating the error plot, ax : axis object, optional Plot in given axis; if None creates a new figure kwargs : Other keyword arguments are passed to main plot() call Returns ------- ax : matplotlib axis axis with plot data """ # Sort out default values for the parameters if ax is None: ax = plt.gca() if err_kws is None: err_kws = {} # Handle different types of input data if isinstance(data, pd.DataFrame): xlabel = time ylabel = value # Condition is optional if condition is None: condition = pd.Series(np.ones(len(data))) legend = False legend_name = None n_cond = 1 else: legend = True and legend legend_name = condition n_cond = len(data[condition].unique()) else: data = np.asarray(data) # Data can be a timecourse from a single unit or # several observations in one condition if data.ndim == 1: data = data[np.newaxis, :, np.newaxis] elif data.ndim == 2: data = data[:, :, np.newaxis] n_unit, n_time, n_cond = data.shape # Units are experimental observations. Maybe subjects, or neurons if unit is None: units = np.arange(n_unit) unit = "unit" units = np.repeat(units, n_time * n_cond) ylabel = None # Time forms the xaxis of the plot if time is None: times = np.arange(n_time) else: times = np.asarray(time) xlabel = None if hasattr(time, "name"): xlabel = time.name time = "time" times = np.tile(np.repeat(times, n_cond), n_unit) # Conditions split the timeseries plots if condition is None: conds = range(n_cond) legend = False if isinstance(color, dict): err = "Must have condition names if using color dict." raise ValueError(err) else: conds = np.asarray(condition) legend = True and legend if hasattr(condition, "name"): legend_name = condition.name else: legend_name = None condition = "cond" conds = np.tile(conds, n_unit * n_time) # Value forms the y value in the plot if value is None: ylabel = None else: ylabel = value value = "value" # Convert to long-form DataFrame data = pd.DataFrame(dict(value=data.ravel(), time=times, unit=units, cond=conds)) # Set up the err_style and ci arguments for teh loop below if isinstance(err_style, string_types): err_style = [err_style] elif err_style is None: err_style = [] if not hasattr(ci, "__iter__"): ci = [ci] # Set up the color palette if color is None: colors = color_palette() elif isinstance(color, dict): colors = [color[c] for c in data[condition].unique()] else: try: colors = color_palette(color, n_cond) except ValueError: color = mpl.colors.colorConverter.to_rgb(color) colors = [color] * n_cond # Do a groupby with condition and plot each trace for c, (cond, df_c) in enumerate(data.groupby(condition, sort=False)): df_c = df_c.pivot(unit, time, value) x = df_c.columns.values.astype(np.float) # Bootstrap the data for confidence intervals boot_data = moss.bootstrap(df_c.values, n_boot=n_boot, axis=0, func=estimator) cis = [moss.ci(boot_data, v, axis=0) for v in ci] central_data = estimator(df_c.values, axis=0) # Get the color for this condition color = colors[c] # Use subroutines to plot the uncertainty for style in err_style: # Allow for null style (only plot central tendency) if style is None: continue # Grab the function from the global environment try: plot_func = globals()["_plot_%s" % style] except KeyError: raise ValueError("%s is not a valid err_style" % style) # Possibly set up to plot each observation in a different color if err_palette is not None and "unit" in style: orig_color = color color = color_palette(err_palette, len(df_c.values)) # Pass all parameters to the error plotter as keyword args plot_kwargs = dict(ax=ax, x=x, data=df_c.values, boot_data=boot_data, central_data=central_data, color=color, err_kws=err_kws) # Plot the error representation, possibly for multiple cis for ci_i in cis: plot_kwargs["ci"] = ci_i plot_func(**plot_kwargs) if err_palette is not None and "unit" in style: color = orig_color # Plot the central trace marker = kwargs.pop("marker", "" if interpolate else "o") linestyle = kwargs.pop("linestyle", "-" if interpolate else "") label = kwargs.pop("label", cond if legend else "_nolegend_") ax.plot(x, central_data, color=color, label=label, marker=marker, linestyle=linestyle, **kwargs) # Pad the sides of the plot only when not interpolating ax.set_xlim(x.min(), x.max()) x_diff = x[1] - x[0] if not interpolate: ax.set_xlim(x.min() - x_diff, x.max() + x_diff) # Add the plot labels if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) if legend: ax.legend(loc=0, title=legend_name) return ax
def tsplot(data, time=None, unit=None, condition=None, value=None, err_style="ci_band", ci=68, interpolate=True, color=None, estimator=np.mean, n_boot=5000, err_palette=None, err_kws=None, legend=True, ax=None, **kwargs): """Plot one or more timeseries with flexible representation of uncertainty. This function can take data specified either as a long-form (tidy) DataFrame or as an ndarray with dimensions for sampling unit, time, and (optionally) condition. The interpretation of some of the other parameters changes depending on the type of object passed as data. Parameters ---------- data : DataFrame or ndarray Data for the plot. Should either be a "long form" dataframe or an array with dimensions (unit, time, condition). In both cases, the condition field/dimension is optional. The type of this argument determines the interpretation of the next few parameters. time : string or series-like Either the name of the field corresponding to time in the data DataFrame or x values for a plot when data is an array. If a Series, the name will be used to label the x axis. value : string Either the name of the field corresponding to the data values in the data DataFrame (i.e. the y coordinate) or a string that forms the y axis label when data is an array. unit : string Field in the data DataFrame identifying the sampling unit (e.g. subject, neuron, etc.). The error representation will collapse over units at each time/condition observation. This has no role when data is an array. condition : string or Series-like Either the name of the field identifying the condition an observation falls under in the data DataFrame, or a sequence of names with a length equal to the size of the third dimension of data. There will be a separate trace plotted for each condition. If condition is a Series with a name attribute, the name will form the title for the plot legend (unless legend is set to False). err_style : string or list of strings or None Names of ways to plot uncertainty across units from set of {ci_band, ci_bars, boot_traces, book_kde, unit_traces, unit_points}. Can use one or more than one method. ci : float or list of floats in [0, 100] Confidence interaval size(s). If a list, it will stack the error plots for each confidence interval. Only relevant for error styles with "ci" in the name. interpolate : boolean Whether to do a linear interpolation between each timepoint when plotting. The value of this parameter also determines the marker used for the main plot traces, unless marker is specified as a keyword argument. color : seaborn palette or matplotlib color name or dictionary Palette or color for the main plots and error representation (unless plotting by unit, which can be separately controlled with err_palette). If a dictionary, should map condition name to color spec. estimator : callable Function to determine central tendency and to pass to bootstrap must take an ``axis`` argument. n_boot : int Number of bootstrap iterations. err_palette: seaborn palette Palette name or list of colors used when plotting data for each unit. err_kws : dict, optional Keyword argument dictionary passed through to matplotlib function generating the error plot, ax : axis object, optional Plot in given axis; if None creates a new figure kwargs : Other keyword arguments are passed to main plot() call Returns ------- ax : matplotlib axis axis with plot data """ # Sort out default values for the parameters if ax is None: ax = plt.gca() if err_kws is None: err_kws = {} # Handle case where data is an array if isinstance(data, pd.DataFrame): xlabel = time ylabel = value # Condition is optional if condition is None: condition = pd.Series(np.ones(len(data))) legend = False legend_name = None n_cond = 1 else: legend = True and legend legend_name = condition n_cond = len(data[condition].unique()) else: data = np.asarray(data) # Data can be a timecourse from a single unit or # several observations in one condition if data.ndim == 1: data = data[np.newaxis, :, np.newaxis] elif data.ndim == 2: data = data[:, :, np.newaxis] n_unit, n_time, n_cond = data.shape # Units are experimental observations. Maybe subjects, or neurons if unit is None: units = np.arange(n_unit) unit = "unit" units = np.repeat(units, n_time * n_cond) ylabel = None # Time forms the xaxis of the plot if time is None: times = np.arange(n_time) else: times = np.asarray(time) xlabel = None if hasattr(time, "name"): xlabel = time.name time = "time" times = np.tile(np.repeat(times, n_cond), n_unit) # Conditions split the timeseries plots if condition is None: conds = range(n_cond) legend = False if isinstance(color, dict): err = "Must have condition names if using color dict." raise ValueError(err) else: conds = np.asarray(condition) legend = True and legend if hasattr(condition, "name"): legend_name = condition.name else: legend_name = None condition = "cond" conds = np.tile(conds, n_unit * n_time) # Value forms the y value in the plot if value is None: ylabel = None else: ylabel = value value = "value" # Convert to long-form DataFrame data = pd.DataFrame( dict(value=data.ravel(), time=times, unit=units, cond=conds)) # Set up the err_style and ci arguments for teh loop below if not hasattr(err_style, "__iter__"): err_style = [err_style] elif err_style is None: err_style = [] if not hasattr(ci, "__iter__"): ci = [ci] # Set up the color palette if color is None: colors = color_palette() elif isinstance(color, dict): colors = [color[c] for c in data[condition].unique()] else: try: colors = color_palette(color, n_cond) except ValueError: color = mpl.colors.colorConverter.to_rgb(color) colors = [color] * n_cond # Do a groupby with condition and plot each trace for c, (cond, df_c) in enumerate(data.groupby(condition, sort=False)): df_c = df_c.pivot(unit, time, value) x = df_c.columns.values.astype(np.float) # Bootstrap the data for confidence intervals boot_data = moss.bootstrap(df_c.values, n_boot=n_boot, axis=0, func=estimator) cis = [moss.ci(boot_data, v, axis=0) for v in ci] central_data = estimator(df_c.values, axis=0) # Get the color for this condition color = colors[c] # Use subroutines to plot the uncertainty for style in err_style: # Allow for null style (only plot central tendency) if style is None: continue # Grab the function from the global environment try: plot_func = globals()["_plot_%s" % style] except KeyError: raise ValueError("%s is not a valid err_style" % style) # Possibly set up to plot each observation in a different color if err_palette is not None and "unit" in style: orig_color = color color = color_palette(err_palette, len(df_c.values)) # Pass all parameters to the error plotter as keyword args plot_kwargs = dict(ax=ax, x=x, data=df_c.values, boot_data=boot_data, central_data=central_data, color=color, err_kws=err_kws) # Plot the error representation, possibly for multiple cis for ci_i in cis: plot_kwargs["ci"] = ci_i plot_func(**plot_kwargs) if err_palette is not None and "unit" in style: color = orig_color # Plot the central trace marker = kwargs.pop("marker", "" if interpolate else "o") linestyle = kwargs.pop("linestyle", "-" if interpolate else "") label = kwargs.pop("label", cond if legend else "_nolegend_") ax.plot(x, central_data, color=color, label=label, marker=marker, linestyle=linestyle, **kwargs) # Pad the sides of the plot only when not interpolating ax.set_xlim(x.min(), x.max()) x_diff = x[1] - x[0] if not interpolate: ax.set_xlim(x.min() - x_diff, x.max() + x_diff) # Add the plot labels if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) if legend: ax.legend(loc=0, title=legend_name) return ax
def regplot(x, y, data=None, corr_func=stats.pearsonr, func_name=None, xlabel="", ylabel="", ci=95, size=None, annotloc=None, color=None, reg_kws=None, scatter_kws=None, dist_kws=None, text_kws=None): """Scatterplot with regresion line, marginals, and correlation value. Parameters ---------- x : sequence or string Independent variable. y : sequence or string Dependent variable. data : dataframe, optional If dataframe is given, x, and y are interpreted as string keys for selecting to dataframe column names. corr_func : callable, optional Correlation function; expected to take two arrays and return a numeric or (statistic, pval) tuple. func_name : string, optional Use in lieu of function name for fit statistic annotation. xlabel, ylabel : string, optional Axis label names if inputs are not Pandas objects or to override. ci : int or None Confidence interval for the regression estimate. size: int Figure size (will be a square; only need one int). annotloc : two or three tuple Specified with (xpos, ypos [, horizontalalignment]). color : matplotlib color scheme Color of everything but the regression line; can be overridden by passing `color` to subfunc kwargs. {reg, scatter, dist, text}_kws: dicts Further keyword arguments for the constituent plots. """ # Interperet inputs if data is not None: if not xlabel: xlabel = x if not ylabel: ylabel = y x = data[x].values y = data[y].values else: if hasattr(x, "name") and not xlabel: if x.name is not None: xlabel = x.name if hasattr(y, "name") and not ylabel: if y.name is not None: ylabel = y.name x = np.asarray(x) y = np.asarray(y) # Set up the figure and axes size = mpl.rcParams["figure.figsize"][1] if size is None else size fig = plt.figure(figsize=(size, size)) ax_scatter = fig.add_axes([0.05, 0.05, 0.75, 0.75]) ax_x_marg = fig.add_axes([0.05, 0.82, 0.75, 0.13]) ax_y_marg = fig.add_axes([0.82, 0.05, 0.13, 0.75]) # Plot the scatter if scatter_kws is None: scatter_kws = {} if color is not None and "color" not in scatter_kws: scatter_kws.update(color=color) marker = scatter_kws.pop("markerstyle", "o") mew = scatter_kws.pop("mew", 0) alpha_maker = stats.norm(0, 100) alpha = alpha_maker.pdf(len(x)) / alpha_maker.pdf(0) alpha = max(alpha, .1) alpha = scatter_kws.pop("alpha", alpha) ax_scatter.plot(x, y, marker, alpha=alpha, mew=mew, **scatter_kws) ax_scatter.set_xlabel(xlabel) ax_scatter.set_ylabel(ylabel) # Marginal plots using our distplot function if dist_kws is None: dist_kws = {} if color is not None and "color" not in dist_kws: dist_kws.update(color=color) dist_kws["axlabel"] = False distplot(x, ax=ax_x_marg, **dist_kws) distplot(y, ax=ax_y_marg, vertical=True, **dist_kws) for ax in [ax_x_marg, ax_y_marg]: ax.set_xticklabels([]) ax.set_yticklabels([]) # Regression line plot xlim = ax_scatter.get_xlim() a, b = np.polyfit(x, y, 1) if reg_kws is None: reg_kws = {} reg_color = reg_kws.pop("color", "#222222") yhat = np.polyval([a, b], xlim) ax_scatter.plot(xlim, yhat, color=reg_color, **reg_kws) # This is a hack to get the annotation to work reg = ax_scatter.plot(xlim, yhat, lw=0) # Bootstrapped regression standard error if ci is not None: xx = np.linspace(xlim[0], xlim[1], 100) def _bootstrap_reg(x, y): fit = np.polyfit(x, y, 1) return np.polyval(fit, xx) boots = moss.bootstrap(x, y, func=_bootstrap_reg) ci_lims = [50 - ci / 2., 50 + ci / 2.] ci_band = moss.percentiles(boots, ci_lims, axis=0) ax_scatter.fill_between(xx, *ci_band, color=reg_color, alpha=.15) ax_scatter.set_xlim(xlim) # Calcluate a fit statistic and p value if func_name is None: func_name = corr_func.__name__ out = corr_func(x, y) try: s, p = out msg = "%s: %.2g (p=%.2g%s)" % (func_name, s, p, moss.sig_stars(p)) except TypeError: s = corr_func(x, y) msg = "%s: %.3f" % (func_name, s) if text_kws is None: text_kws = {} ax_scatter.legend(reg, [msg], loc="best", prop=text_kws) # Set the axes on the marginal plots ax_x_marg.set_xlim(ax_scatter.get_xlim()) ax_x_marg.set_yticks([]) ax_y_marg.set_ylim(ax_scatter.get_ylim()) ax_y_marg.set_xticks([])
def lmplot(x, y, data, color=None, row=None, col=None, col_wrap=None, x_estimator=None, x_ci=95, x_bins=None, n_boot=5000, fit_reg=True, order=1, ci=95, logistic=False, truncate=False, x_partial=None, y_partial=None, x_jitter=None, y_jitter=None, sharex=True, sharey=True, palette="husl", size=None, scatter_kws=None, line_kws=None, palette_kws=None): """Plot a linear model with faceting, color binning, and other options. Parameters ---------- x, y : strings Column names in `data` DataFrame for x and y variables. data : DataFrame Dource of data for the model. color : string, optional DataFrame column name to group the model by color. row, col : strings, optional DataFrame column names to make separate plot facets. col_wrap : int, optional Wrap col variable at this width - cannot be used with row facet. x_estimator : callable, optional Interpret X values as factor labels and use this function to plot the point estimate and bootstrapped CI. x_ci : int optional Size of confidence interval for x_estimator error bars. x_bins : sequence of floats, optional Bin the x variable with these values. Implies that x_estimator is mean, unless otherwise provided. n_boot : int, optional Number of bootstrap iterations to perform. fit_reg : bool, optional If True fit a regression model by color/row/col and plot. order : int, optional Order of the regression polynomial to fit. ci : int, optional Confidence interval for the regression line. logistic : bool, optional Fit the regression line with logistic regression. truncate : bool, optional If True, only fit line from data min to data max. {x, y}_partial : string or list of strings, optional Regress these variables out of the factors before plotting. {x, y}_jitter : float, optional Parameters for uniformly distributed random noise added to positions. sharex, sharey : bools, optional Only relevant if faceting; passed to plt.subplots. palette : seaborn color palette argument If using separate plots by color, draw with this color palette. size : float, optional Size (plots are square) for each plot facet. {scatter, line}_kws : dictionary Keyword arguments to pass to the underlying plot functions. palette_kws : dictionary Keyword arguments for seaborn.color_palette. """ # TODO # - legend when fit_line is False # First sort out the general figure layout if size is None: size = mpl.rcParams["figure.figsize"][1] if col is None and col_wrap is not None: raise ValueError("Need column facet variable for `col_wrap`") if row is not None and col_wrap is not None: raise ValueError("Cannot facet rows when using `col_wrap`") nrow = 1 if row is None else len(data[row].unique()) ncol = 1 if col is None else len(data[col].unique()) if col_wrap is not None: ncol = col_wrap nrow = int(np.ceil(len(data[col].unique()) / col_wrap)) f, axes = plt.subplots(nrow, ncol, sharex=sharex, sharey=sharey, figsize=(size * ncol, size * nrow)) axes = np.atleast_2d(axes).reshape(nrow, ncol) if nrow == 1 or col_wrap is not None: row_masks = [np.repeat(True, len(data))] else: row_vals = np.sort(data[row].unique()) row_masks = [data[row] == val for val in row_vals] if ncol == 1: col_masks = [np.repeat(True, len(data))] else: col_vals = np.sort(data[col].unique()) col_masks = [data[col] == val for val in col_vals] if x_bins is not None: x_estimator = np.mean if x_estimator is None else x_estimator x_bins = np.c_[x_bins] if x_partial is not None: if not isinstance(x_partial, list): x_partial = [x_partial] if y_partial is not None: if not isinstance(y_partial, list): y_partial = [y_partial] if palette_kws is None: palette_kws = {} # Sort out the plot colors color_factor = color if color is None: hue_masks = [np.repeat(True, len(data))] colors = ["#222222"] else: hue_vals = np.sort(data[color].unique()) hue_masks = [data[color] == val for val in hue_vals] colors = color_palette(palette, len(hue_masks), **palette_kws) # Default keyword arguments for plot components if scatter_kws is None: scatter_kws = {} if line_kws is None: line_kws = {} # First walk through the facets and plot the scatters scatter_ms = scatter_kws.pop("ms", 4) scatter_mew = mew = scatter_kws.pop("mew", 0) scatter_alpha = mew = scatter_kws.pop("alpha", .77) for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): if col_wrap is not None: f_row = col_j // ncol f_col = col_j % ncol else: f_row, f_col = row_i, col_j ax = axes[f_row, f_col] if f_row + 1 == nrow: ax.set_xlabel(x) if f_col == 0: ax.set_ylabel(y) # Title the plot if we are faceting title = "" if row is not None: title += "%s = %s" % (row, row_vals[row_i]) if row is not None and col is not None: title += " | " if col is not None: title += "%s = %s" % (col, col_vals[col_j]) if size < 3: title = title.replace(" | ", "\n") ax.set_title(title) for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] if x_estimator is not None: ms = scatter_kws.pop("ms", 7) mew = scatter_kws.pop("mew", 0) if x_bins is None: x_vals = data_ijk[x].unique() x_data = data_ijk[x] else: dist = distance.cdist(np.c_[data_ijk[x]], x_bins) x_vals = x_bins.ravel() x_data = x_bins[np.argmin(dist, axis=1)].ravel() y_vals = data_ijk[y] if y_partial is not None: for var in y_partial: conf = data_ijk[var] conf -= conf.mean() y_mean = y_vals.mean() y_vals = moss.vector_reject(y_vals - y_mean, conf) y_vals += y_mean y_grouped = [np.array(y_vals[x_data == v]) for v in x_vals] y_est = [x_estimator(y_i) for y_i in y_grouped] y_boots = [moss.bootstrap(np.array(y_i), func=x_estimator, n_boot=n_boot) for y_i in y_grouped] ci_lims = [50 - x_ci / 2., 50 + x_ci / 2.] y_ci = [moss.percentiles(y_i, ci_lims) for y_i in y_boots] y_error = ci_to_errsize(np.transpose(y_ci), y_est) ax.plot(x_vals, y_est, "o", mew=mew, ms=ms, color=color, **scatter_kws) ax.errorbar(x_vals, y_est, y_error, fmt=None, ecolor=color) else: x_ = data_ijk[x] y_ = data_ijk[y] if x_partial is not None: for var in x_partial: conf = data_ijk[var] conf -= conf.mean() x_mean = x_.mean() x_ = moss.vector_reject(x_ - x_mean, conf) x_ += x_mean if y_partial is not None: for var in y_partial: conf = data_ijk[var] conf -= conf.mean() y_mean = y_.mean() y_ = moss.vector_reject(y_ - y_mean, conf) y_ += y_mean if x_jitter is not None: x_ += np.random.uniform(-x_jitter, x_jitter, x_.shape) if y_jitter is not None: y_ += np.random.uniform(-y_jitter, y_jitter, y_.shape) ax.plot(x_, y_, "o", color=color, alpha=scatter_alpha, mew=scatter_mew, ms=scatter_ms, **scatter_kws) for ax_i in np.ravel(axes): ax_i.set_xmargin(.05) ax_i.autoscale_view() # Now walk through again and plot the regression estimate # and a confidence interval for the regression line if fit_reg: for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): if col_wrap is not None: f_row = col_j // ncol f_col = col_j % ncol else: f_row, f_col = row_i, col_j ax = axes[f_row, f_col] xlim = ax.get_xlim() for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] x_vals = np.array(data_ijk[x]) y_vals = np.array(data_ijk[y]) if not len(x_vals): continue # Sort out the limit of the fit if truncate: xx = np.linspace(x_vals.min(), x_vals.max(), 100) else: xx = np.linspace(xlim[0], xlim[1], 100) xx_ = sm.add_constant(xx, prepend=True) # Inner function to bootstrap the regression def _regress(x, y): if logistic: x_ = sm.add_constant(x, prepend=True) fit = sm.GLM(y, x_, family=sm.families.Binomial()).fit() reg = fit.predict(xx_) else: fit = np.polyfit(x, y, order) reg = np.polyval(fit, xx) return reg # Remove nuisance variables with vector rejection if x_partial is not None: for var in x_partial: conf = data_ijk[var] conf -= conf.mean() x_mean = x_vals.mean() x_vals = moss.vector_reject(x_vals - x_mean, conf) x_vals += x_mean if y_partial is not None: for var in y_partial: conf = data_ijk[var] conf -= conf.mean() y_mean = y_vals.mean() y_vals = moss.vector_reject(y_vals - y_mean, conf) y_vals += y_mean # Regression line confidence interval if ci is not None: ci_lims = [50 - ci / 2., 50 + ci / 2.] boots = moss.bootstrap(x_vals, y_vals, func=_regress, n_boot=n_boot) ci_band = moss.percentiles(boots, ci_lims, axis=0) ax.fill_between(xx, *ci_band, color=color, alpha=.15) # Regression line reg = _regress(x_vals, y_vals) if color_factor is None: label = "" else: label = hue_vals[hue_k] ax.plot(xx, reg, color=color, label=str(label), **line_kws) ax.set_xlim(xlim) # Plot the legend on the upper left facet and adjust the layout if color_factor is not None and color_factor not in [row, col]: axes[0, 0].legend(loc="best", title=color_factor) plt.tight_layout()
def tsplot(x, data, err_style="ci_band", ci=68, interpolate=True, estimator=np.mean, n_boot=10000, smooth=False, err_palette=None, ax=None, err_kws=None, **kwargs): """Plot timeseries from a set of observations. Parameters ---------- x : n_tp array x values data : n_obs x n_tp array array of timeseries data where first axis is observations. other objects (e.g. DataFrames) are converted to an array if possible err_style : string or list of strings names of ways to plot uncertainty across observations from set of {ci_band, ci_bars, boot_traces, book_kde, obs_traces, obs_points} ci : int or list of ints confidence interaval size(s). if a list, it will stack the error plots for each confidence interval estimator : callable function to determine centralt tendency and to pass to bootstrap must take an ``axis`` argument n_boot : int number of bootstrap iterations smooth : boolean whether to perform a smooth bootstrap (resample from KDE) ax : axis object, optional plot in given axis; if None creates a new figure err_kws : dict, optional keyword argument dictionary passed through to matplotlib function generating the error plot kwargs : further keyword arguments for main call to plot() Returns ------- ax : matplotlib axis axis with plot data """ if ax is None: ax = plt.gca() if err_kws is None: err_kws = {} # Bootstrap the data for confidence intervals data = np.asarray(data) boot_data = moss.bootstrap(data, n_boot=n_boot, smooth=smooth, axis=0, func=estimator) ci_list = hasattr(ci, "__iter__") if not ci_list: ci = [ci] ci_vals = [(50 - w / 2, 50 + w / 2) for w in ci] cis = [moss.percentiles(boot_data, v, axis=0) for v in ci_vals] central_data = estimator(data, axis=0) # Plot the timeseries line to get its color line, = ax.plot(x, central_data, **kwargs) color = line.get_color() line.remove() kwargs.pop("color", None) # Use subroutines to plot the uncertainty if not hasattr(err_style, "__iter__"): err_style = [err_style] for style in err_style: # Grab the function from the global environment try: plot_func = globals()["_plot_%s" % style] except KeyError: raise ValueError("%s is not a valid err_style" % style) # Possibly set up to plot each observation in a different color if err_palette is not None and "obs" in style: orig_color = color color = color_palette(err_palette, len(data), desat=.99) plot_kwargs = dict(ax=ax, x=x, data=data, boot_data=boot_data, central_data=central_data, color=color, err_kws=err_kws) for ci_i in cis: plot_kwargs["ci"] = ci_i plot_func(**plot_kwargs) if err_palette is not None and "obs" in style: color = orig_color # Replot the central trace so it is prominent marker = kwargs.pop("marker", "" if interpolate else "o") linestyle = kwargs.pop("linestyle", "-" if interpolate else "") ax.plot(x, central_data, color=color, marker=marker, linestyle=linestyle, **kwargs) return ax
X = column_stack((randn(1000, 4), ones(1000))) w = rand(5) # Large sample high noise; N = 1000, noise std = 5 y_large_n = dot(X, w) + randn(1000) * 5 # Small sample low noise; high signal to noise ratio y_lownoise = dot(X[:100], w) + randn(100) # Small sample, high noise y_noisy = dot(X[:100], w) + randn(100) * 5 # Bootstrap all three models n_boot = 1000 w_boot1 = moss.bootstrap(X, y_large_n, n_boot=n_boot, func=ols_fit) w_boot2 = moss.bootstrap(X[:100], y_lownoise, n_boot=n_boot, func=ols_fit) w_boot3 = moss.bootstrap(X[:100], y_noisy, n_boot=n_boot, func=ols_fit) # The median of these bootstrapped estimates are the weights for each model. # You could also use the mean if you prefer. w_model1 = median(w_boot1, axis=0) w_model2 = median(w_boot2, axis=0) w_model3 = median(w_boot3, axis=0) # Compute the 95% confidence intervals on parameter estimates pcts = [2.5, 97.5] ci1 = moss.percentiles(w_boot1, pcts, axis=0) ci2 = moss.percentiles(w_boot2, pcts, axis=0) ci3 = moss.percentiles(w_boot3, pcts, axis=0)
def lmplot(x, y, data, color=None, row=None, col=None, x_estimator=None, x_ci=95, fit_line=True, ci=95, truncate=False, sharex=True, sharey=True, palette="hls", size=None, scatter_kws=None, line_kws=None, palette_kws=None): """Plot a linear model from a DataFrame. Parameters ---------- x, y : strings column names in `data` DataFrame for x and y variables data : DataFrame source of data for the model color : string, optional DataFrame column name to group the model by color row, col : strings, optional DataFrame column names to make separate plot facets x_estimator : callable, optional Interpret X values as factor labels and use this function to plot the point estimate and bootstrapped CI x_ci : int optional size of confidence interval for x_estimator error bars fit_line : bool, optional if True fit a regression line by color/row/col and plot ci : int, optional confidence interval for the regression line truncate : bool, optional if True, only fit line from data min to data max sharex, sharey : bools, optional only relevant if faceting; passed to plt.subplots palette : seaborn color palette argument if using separate plots by color, draw with this color palette size : float, optional size (plots are square) for each plot facet {scatter, line}_kws : dictionary keyword arguments to pass to the underlying plot functions palette_kws : dictionary keyword arguments for seaborn.color_palette """ # TODO # - position_{dodge, jitter} # - legend when fit_line is False # - truncate fit # - wrap title when wide # - wrap columns # First sort out the general figure layout if size is None: size = mpl.rcParams["figure.figsize"][1] nrow = 1 if row is None else len(data[row].unique()) ncol = 1 if col is None else len(data[col].unique()) f, axes = plt.subplots(nrow, ncol, sharex=sharex, sharey=sharey, figsize=(size * ncol, size * nrow)) axes = np.atleast_2d(axes).reshape(nrow, ncol) if nrow == 1: row_masks = [np.repeat(True, len(data))] else: row_vals = np.sort(data[row].unique()) row_masks = [data[row] == val for val in row_vals] if ncol == 1: col_masks = [np.repeat(True, len(data))] else: col_vals = np.sort(data[col].unique()) col_masks = [data[col] == val for val in col_vals] if palette_kws is None: palette_kws = {} # Sort out the plot colors color_factor = color if color is None: hue_masks = [np.repeat(True, len(data))] colors = ["#222222"] else: hue_vals = np.sort(data[color].unique()) hue_masks = [data[color] == val for val in hue_vals] colors = color_palette(palette, len(hue_masks), **palette_kws) # Default keyword arguments for plot components if scatter_kws is None: scatter_kws = {} if line_kws is None: line_kws = {} # First walk through the facets and plot the scatters for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): ax = axes[row_i, col_j] if not sharex or (row_i + 1 == len(row_masks)): ax.set_xlabel(x) if not sharey or col_j == 0: ax.set_ylabel(y) # Title the plot if we are faceting title = "" if row is not None: title += "%s = %s" % (row, row_vals[row_i]) if row is not None and col is not None: title += " | " if col is not None: title += "%s = %s" % (col, col_vals[col_j]) ax.set_title(title) for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] if x_estimator is not None: ms = scatter_kws.pop("ms", 7) mew = scatter_kws.pop("mew", 0) x_vals = data_ijk[x].unique() y_grouped = [np.array(data_ijk[y][data_ijk[x] == v]) for v in x_vals] y_est = [x_estimator(y_i) for y_i in y_grouped] y_boots = [moss.bootstrap(np.array(y_i), func=x_estimator) for y_i in y_grouped] ci_lims = [50 - x_ci / 2., 50 + x_ci / 2.] y_ci = [moss.percentiles(y_i, ci_lims) for y_i in y_boots] y_error = ci_to_errsize(np.transpose(y_ci), y_est) ax.plot(x_vals, y_est, "o", mew=mew, ms=ms, color=color, **scatter_kws) ax.errorbar(x_vals, y_est, y_error, fmt=None, ecolor=color) else: ms = scatter_kws.pop("ms", 4) mew = scatter_kws.pop("mew", 0) ax.plot(data_ijk[x], data_ijk[y], "o", color=color, mew=mew, ms=ms, **scatter_kws) for ax_i in np.ravel(axes): ax_i.set_xmargin(.05) ax_i.autoscale_view() # Now walk through again and plot the regression estimate # and a confidence interval for the regression line if fit_line: for row_i, row_mask in enumerate(row_masks): for col_j, col_mask in enumerate(col_masks): ax = axes[row_i, col_j] xlim = ax.get_xlim() for hue_k, hue_mask in enumerate(hue_masks): color = colors[hue_k] data_ijk = data[row_mask & col_mask & hue_mask] x_vals = np.array(data_ijk[x]) y_vals = np.array(data_ijk[y]) # Sort out the limit of the fit if truncate: xx = np.linspace(x_vals.min(), x_vals.max(), 100) else: xx = np.linspace(xlim[0], xlim[1], 100) # Inner function to bootstrap the regression def _bootstrap_reg(x, y): fit = np.polyfit(x, y, 1) return np.polyval(fit, xx) # Regression line confidence interval if ci is not None: ci_lims = [50 - ci / 2., 50 + ci / 2.] boots = moss.bootstrap(x_vals, y_vals, func=_bootstrap_reg) ci_band = moss.percentiles(boots, ci_lims, axis=0) ax.fill_between(xx, *ci_band, color=color, alpha=.15) fit = np.polyfit(x_vals, y_vals, 1) reg = np.polyval(fit, xx) if color_factor is None: label = "" else: label = hue_vals[hue_k] ax.plot(xx, reg, color=color, label=str(label), **line_kws) ax.set_xlim(xlim) # Plot the legend on the upper left facet and adjust the layout if color_factor is not None: axes[0, 0].legend(loc="best", title=color_factor) plt.tight_layout()