def twinhist(X, Y, ax=None, xbins=10, ybins=10, fmt='o', color='b', histalpha=0.5, xlabel=None, ylabel=None, xlim=None, ylim=None, **kwargs): """ Plot a twin histogram.""" from barak.plot import hist_xedge, hist_yedge if ax is None: ax = pl.gca() ax.plot(X,Y, fmt, color=color, **kwargs) hist_xedge(X,ax, bins=xbins,fill=True, color=color, alpha=histalpha) hist_yedge(Y,ax, bins=ybins,fill=True, color=color, alpha=histalpha) if xlim is not None: ax.set_xlim(*xlim) if ylim is not None: ax.set_ylim(*ylim) if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) return ax
def plot_posteriors(chain, P, nplot='all'): """ Plot the posterior distributions for a series of parameter samples. chain has shape (nsample, nparameters). """ if nplot == 'all': nplot = chain.shape[-1] #nrows, ncols = get_nrows_ncols(nplot) #fig,axes = get_fig_axes(nrows, ncols, nplot) fig = pl.figure(figsize=(8.4, 8.4)) fig.subplots_adjust(left=0.05, bottom=0.05, hspace=0.001, wspace=0.001) axes = [] pl.rc('xtick', labelsize=8) pl.rc('ytick', labelsize=8) for i0 in xrange(nplot): for i1 in xrange(nplot): if i0 == i1:# or i1 < i0: continue ax = fig.add_subplot(nplot,nplot, i0 * nplot + i1 + 1) y,x = chain[:,i0], chain[:,i1] ax.plot(x,y,'r.', ms=1, mew=0)#, alpha=0.5) #y,x = chain[:,i0][P['ijoint_sig'][1]], chain[:,i1][P['ijoint_sig'][1]] #ax.plot(x,y,'g.', ms=1.5, mew=0) #y,x = chain[:,i0][P['ijoint_sig'][0]], chain[:,i1][P['ijoint_sig'][0]] #ax.plot(x,y,'r.', ms=1.5, mew=0) ax.plot(P['ml'][i1], P['ml'][i0], 'xk', ms=8, mew=2) ax.plot(P['ml'][i1], P['ml'][i0], 'xr', ms=6, mew=1) puttext(0.05, 0.95, P['names'][i0], ax, fontsize=12, va='top') puttext(0.95, 0.05, P['names'][i1], ax, fontsize=12, ha='right') y0, y1 = np.percentile(chain[:,i0], [5, 95]) dy = y1 - y0 ax.set_ylim(y0 - dy, y1 + dy) x0, x1 = np.percentile(chain[:,i1], [5, 95]) dx = x1 - x0 ax.set_xlim(x0 - dx, x1 + dx) c = 'crimson' if i0 == 0: ax.xaxis.set_tick_params(labeltop='on') ax.xaxis.set_tick_params(labelbottom='off') for t in ax.get_xticklabels(): t.set_rotation(60) elif i0 == nplot-1 or (i0 == nplot-2 and i1 == nplot-1): hist_xedge(chain[:, i1], ax, fmt='b', bins=P['bins'][i1], loc='bottom') ax.axvline(P['p1sig'][i1][0], ymax=0.2, color=c, lw=0.5) ax.axvline(P['p1sig'][i1][1], ymax=0.2, color=c, lw=0.5) ax.axvline(P['median'][i1], ymax=0.2, color=c, lw=1.5) for t in ax.get_xticklabels(): t.set_rotation(60) else: ax.set_xticklabels('') if not (i1 == 0 or (i0 == 0 and i1 == 1) or i1 == nplot-1): ax.set_yticklabels('') if (i0 == 0 and i1 == 1) or i1 == 0: hist_yedge(chain[:, i0], ax, fmt='b', bins=P['bins'][i0], loc='left') ax.axhline(P['p1sig'][i0][0], xmax=0.2, color=c, lw=0.5) ax.axhline(P['p1sig'][i0][1], xmax=0.2, color=c, lw=0.5) ax.axhline(P['median'][i0], xmax=0.2, color=c, lw=1.5) if i1 == nplot - 1: ax.yaxis.set_tick_params(labelright='on') ax.yaxis.set_tick_params(labelleft='off') axes.append(ax) return fig, axes
def plot_posteriors(chain, P, nplot='all'): """ Plot the posterior distributions for a series of parameter samples. chain has shape (nsample, nparameters). """ if nplot == 'all': nplot = chain.shape[-1] #nrows, ncols = get_nrows_ncols(nplot) #fig,axes = get_fig_axes(nrows, ncols, nplot) fig = pl.figure(figsize=(8.4, 8.4)) fig.subplots_adjust(left=0.05, bottom=0.06, hspace=0.001, wspace=0.001) axes = [] pl.rc('xtick', labelsize=8) pl.rc('ytick', labelsize=8) for i0 in xrange(nplot): for i1 in xrange(nplot): if i0 == i1: # or i1 < i0: # uncomment to keep just one triangle. continue ax = fig.add_subplot(nplot, nplot, i0 * nplot + i1 + 1) y, x = chain[:, i0], chain[:, i1] ax.plot(x, y, 'r.', ms=1, mew=0) #, alpha=0.5) #y,x = chain[:,i0][P['ijoint_sig'][1]], chain[:,i1][P['ijoint_sig'][1]] #ax.plot(x,y,'g.', ms=1.5, mew=0) #y,x = chain[:,i0][P['ijoint_sig'][0]], chain[:,i1][P['ijoint_sig'][0]] #ax.plot(x,y,'r.', ms=1.5, mew=0) ax.plot(P['ml'][i1], P['ml'][i0], 'xk', ms=8, mew=2) ax.plot(P['ml'][i1], P['ml'][i0], 'xr', ms=6, mew=1) puttext(0.05, 0.95, P['names'][i0], ax, fontsize=12, va='top') puttext(0.95, 0.05, P['names'][i1], ax, fontsize=12, ha='right') y0, y1 = np.percentile(chain[:, i0], [5, 95]) dy = y1 - y0 ax.set_ylim(y0 - dy, y1 + dy) x0, x1 = np.percentile(chain[:, i1], [5, 95]) dx = x1 - x0 ax.set_xlim(x0 - dx, x1 + dx) c = 'crimson' if i0 == 0: ax.xaxis.set_tick_params(labeltop='on') ax.xaxis.set_tick_params(labelbottom='off') for t in ax.get_xticklabels(): t.set_rotation(60) elif i0 == nplot - 1 or (i0 == nplot - 2 and i1 == nplot - 1): hist_xedge(chain[:, i1], ax, fmt='b', bins=P['bins'][i1], loc='bottom') ax.axvline(P['p1sig'][i1][0], ymax=0.2, color=c, lw=0.5) ax.axvline(P['p1sig'][i1][1], ymax=0.2, color=c, lw=0.5) ax.axvline(P['median'][i1], ymax=0.2, color=c, lw=1.5) for t in ax.get_xticklabels(): t.set_rotation(60) else: ax.set_xticklabels('') if not (i1 == 0 or (i0 == 0 and i1 == 1) or i1 == nplot - 1): ax.set_yticklabels('') if (i0 == 0 and i1 == 1) or i1 == 0: hist_yedge(chain[:, i0], ax, fmt='b', bins=P['bins'][i0], loc='left') ax.axhline(P['p1sig'][i0][0], xmax=0.2, color=c, lw=0.5) ax.axhline(P['p1sig'][i0][1], xmax=0.2, color=c, lw=0.5) ax.axhline(P['median'][i0], xmax=0.2, color=c, lw=1.5) if i1 == nplot - 1: ax.yaxis.set_tick_params(labelright='on') ax.yaxis.set_tick_params(labelleft='off') axes.append(ax) return fig, axes
def plot_posteriors(chain, P, nplot="all"): """ Plot the posterior distributions for a series of parameter samples. chain has shape (nsample, nparameters). """ if nplot == "all": nplot = chain.shape[-1] # nrows, ncols = get_nrows_ncols(nplot) # fig,axes = get_fig_axes(nrows, ncols, nplot) fig = pl.figure(figsize=(8.4, 8.4)) fig.subplots_adjust(left=0.05, bottom=0.06, hspace=0.001, wspace=0.001) axes = [] pl.rc("xtick", labelsize=8) pl.rc("ytick", labelsize=8) for i0 in xrange(nplot): for i1 in xrange(nplot): if i0 == i1: # or i1 < i0: # uncomment to keep just one triangle. continue ax = fig.add_subplot(nplot, nplot, i0 * nplot + i1 + 1) y, x = chain[:, i0], chain[:, i1] ax.plot(x, y, "r.", ms=1, mew=0) # , alpha=0.5) # y,x = chain[:,i0][P['ijoint_sig'][1]], chain[:,i1][P['ijoint_sig'][1]] # ax.plot(x,y,'g.', ms=1.5, mew=0) # y,x = chain[:,i0][P['ijoint_sig'][0]], chain[:,i1][P['ijoint_sig'][0]] # ax.plot(x,y,'r.', ms=1.5, mew=0) ax.plot(P["ml"][i1], P["ml"][i0], "xk", ms=8, mew=2) ax.plot(P["ml"][i1], P["ml"][i0], "xr", ms=6, mew=1) puttext(0.05, 0.95, P["names"][i0], ax, fontsize=12, va="top") puttext(0.95, 0.05, P["names"][i1], ax, fontsize=12, ha="right") y0, y1 = np.percentile(chain[:, i0], [5, 95]) dy = y1 - y0 ax.set_ylim(y0 - dy, y1 + dy) x0, x1 = np.percentile(chain[:, i1], [5, 95]) dx = x1 - x0 ax.set_xlim(x0 - dx, x1 + dx) c = "crimson" if i0 == 0: ax.xaxis.set_tick_params(labeltop="on") ax.xaxis.set_tick_params(labelbottom="off") for t in ax.get_xticklabels(): t.set_rotation(60) elif i0 == nplot - 1 or (i0 == nplot - 2 and i1 == nplot - 1): hist_xedge(chain[:, i1], ax, fmt="b", bins=P["bins"][i1], loc="bottom") ax.axvline(P["p1sig"][i1][0], ymax=0.2, color=c, lw=0.5) ax.axvline(P["p1sig"][i1][1], ymax=0.2, color=c, lw=0.5) ax.axvline(P["median"][i1], ymax=0.2, color=c, lw=1.5) for t in ax.get_xticklabels(): t.set_rotation(60) else: ax.set_xticklabels("") if not (i1 == 0 or (i0 == 0 and i1 == 1) or i1 == nplot - 1): ax.set_yticklabels("") if (i0 == 0 and i1 == 1) or i1 == 0: hist_yedge(chain[:, i0], ax, fmt="b", bins=P["bins"][i0], loc="left") ax.axhline(P["p1sig"][i0][0], xmax=0.2, color=c, lw=0.5) ax.axhline(P["p1sig"][i0][1], xmax=0.2, color=c, lw=0.5) ax.axhline(P["median"][i0], xmax=0.2, color=c, lw=1.5) if i1 == nplot - 1: ax.yaxis.set_tick_params(labelright="on") ax.yaxis.set_tick_params(labelleft="off") axes.append(ax) return fig, axes
def triplot(names, vals, sigvals, fig, indirect={}, labels=None, fontsize=14): from barak.plot import hist_yedge, hist_xedge, puttext npar = len(names) bins = {} for n in names: x0, x1 = vals[n].min(), vals[n].max() dx = x1 - x0 lo = x0 - 0.1*dx hi = x1 + 0.1*dx bins[n] = np.linspace(lo, hi, 20) axes = {} for i0,n0 in enumerate(names): for i1,n1 in enumerate(names): if i0 == i1:# or i1 < i0: # uncomment to keep just one triangle. continue ax = fig.add_subplot(npar,npar, i0 * npar + i1 + 1) ax.locator_params(tight=True, nbins=8) ax.xaxis.set_minor_locator(AutoMinorLocator()) ax.yaxis.set_minor_locator(AutoMinorLocator()) axes[(n0 + ' ' + n1)] = ax y,x = vals[n0], vals[n1] if USE_HEXBIN: ax.hexbin(x,y,cmap=CM, gridsize=40,linewidths=0.1) else: ax.plot(x,y,'r.', ms=0.5, mew=0)#, alpha=0.5) color = 'k' if n0 not in indirect else 'g' text = labels[n0] if labels is not None else n0 puttext(0.05, 0.95, text, ax, color=color ,fontsize=fontsize, va='top') color = 'k' if n1 not in indirect else 'g' text = labels[n1] if labels is not None else n1 puttext(0.95, 0.08, text, ax, color=color ,fontsize=fontsize, ha='right') # set limits y0, y1 = np.percentile(vals[n0], [5, 95]) dy = y1 - y0 ax.set_ylim(y0 - dy, y1 + dy) x0, x1 = np.percentile(vals[n1], [5, 95]) dx = x1 - x0 ax.set_xlim(x0 - dx, x1 + dx) c = 'k' if i0 == 0: ax.xaxis.set_tick_params(labeltop='on') ax.xaxis.set_tick_params(labelbottom='off') for t in ax.get_xticklabels(): t.set_rotation(60) elif i0 == npar-1 or (i0 == npar-2 and i1 == npar-1): hist_xedge(vals[n1], ax, color='forestgreen', fill=dict(color='forestgreen',alpha=0.3), bins=bins[n1], loc='bottom') ax.axvline(sigvals[n1][0], ymax=0.2, color=c, lw=0.5) ax.axvline(sigvals[n1][1], ymax=0.2, color=c, lw=0.5) cen = sum(sigvals[n1]) / 2. ax.axvline(cen, ymax=0.2, color=c, lw=1.5) for t in ax.get_xticklabels(): t.set_rotation(60) else: ax.set_xticklabels('') if not (i1 == 0 or (i0 == 0 and i1 == 1) or i1 == npar-1): ax.set_yticklabels('') if (i0 == 0 and i1 == 1) or i1 == 0: hist_yedge(vals[n0], ax, color='forestgreen', fill=dict(color='forestgreen',alpha=0.3), bins=bins[n0], loc='left') ax.axhline(sigvals[n0][0], xmax=0.2, color=c, lw=0.5) ax.axhline(sigvals[n0][1], xmax=0.2, color=c, lw=0.5) cen = sum(sigvals[n0]) / 2. ax.axhline(cen, xmax=0.2, color=c, lw=1.5) if i1 == npar - 1: ax.yaxis.set_tick_params(labelright='on') ax.yaxis.set_tick_params(labelleft='off') #ax.minorticks_on() return axes