def plot_2E(ax: plt.Axes, bin_edges_human: np.ndarray = np.array( [-1000, -90, -60, -30, -4, -2, 0, 2, 4, 30, 60, 1000]), bin_edges_model: np.ndarray = np.linspace(-160, 100, 40)): Δ, p_human, p_model = [], [], [] for pid in DataExp1.pids: data = DataExp1(pid) model = data.build_model(models.BayesianIdealObserver) df = model.predict(model.fit()) δ = np.stack([ np.log(df[s]) - np.log(np.sum(df.loc[:, df.columns != s], axis=1)) for s in data.structures ]) Δ += list(δ.T.flatten()) p_model += list( data.cross_validate(models.ChoiceModel4Param).to_numpy().flatten()) p_human += list( np.array([data.df['choice'] == s for s in Exp1.structures]).T.flatten()) df = pd.DataFrame({'Δ': Δ, 'p_human': p_human, 'p_model': p_model}) x_human, y_human, yerr_human, x_model, y_model, yerr_model = [], [], [], [], [], [] df['bin'] = pd.cut(df['Δ'], bin_edges_human, labels=False) for i in range(len(bin_edges_human) - 1): _df = df[df['bin'] == i] x_human.append(_df['Δ'].mean()) y_human.append(_df['p_human'].mean()) yerr_human.append(_df['p_human'].sem()) df['bin'] = pd.cut(df['Δ'], bin_edges_model, labels=False) for i in range(len(bin_edges_model) - 1): _df = df[df['bin'] == i] x_model.append(_df['Δ'].mean()) y_model.append(_df['p_model'].mean()) yerr_model.append(_df['p_model'].sem()) ax.errorbar(x_human, y_human, yerr_human, label='Human ± sem', color=colors['decision_human'], fmt='.', capsize=2, ms=2, capthick=0.5, zorder=1) ax.plot(x_model, y_model, color=colors['decision_model'], label='Model', ms=1, zorder=0) ax.set_xlabel(r'logit( $P_\mathregular{ideal}$($S\,|\,\bf{X}$) )') ax.set_ylabel(r'$P($choice=$S\,|\,\bf{X}$)') ax.set_ylim(0, 1) handles, labels = ax.get_legend_handles_labels() ax.legend(handles[::-1], labels[::-1], loc='upper left', handler_map={ErrorbarContainer: HandlerErrorbar(yerr_size=0.25)}) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) plt.tight_layout()
def bayesian_confidence(ax: plt.Axes, bin_edges: np.ndarray = np.array([-1000, -40, -20, -10, -4.5, -1.5, 1.5, 4.5, 10, 20, 40, 1000])): x, y = [], [] for pid in DataExp1.pids: data = DataExp1(pid) model = data.build_model(models.ChoiceModel4Param) L = model.L + model.L_uniform x += list(logsumexp(L, b=model.is_chosen, axis=1) - logsumexp(L, b=1 - model.is_chosen, axis=1)) y += list((model.df['confidence'] == 'high').astype(float)) df = pd.DataFrame({'x': x, 'y': y}) df['bin'] = pd.cut(df['x'], bin_edges, labels=False) x, y, yerr = [], [], [] for i in range(len(bin_edges) - 1): _df = df[df['bin'] == i] if len(_df) == 0: continue x.append(_df['x'].mean()) y.append(_df['y'].mean()) yerr.append(_df['y'].sem()) ax.errorbar(x, y, yerr, label='Human $\pm$ sem', c='darkgreen', fmt='.-', capsize=2, ms=2, capthick=0.5) ax.set_yticks([0, 1]) ax.set_yticklabels(['Low', 'High']) ax.set_ylabel('Avg. reported\nconfidence', labelpad=-16) # ax.set_xticks([0, 0.5, 1]) ax.set_xlabel(r'logit$(\,P_{\mathregular{ideal}}(S\,|\,\bf{X}$$)\,)$') ax.legend(loc='lower right', handler_map={ErrorbarContainer: HandlerErrorbar(yerr_size=0.25)}) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) plt.tight_layout()
def exp2all(): n_col = len(DataExp2.pids) // 3 _, axes = plt.subplots(3, n_col, figsize=(7.5, 5)) h, l = None, None for i in range(len(DataExp2.pids)): pid = DataExp2.pids[i] ax = axes[i // n_col, i % n_col] ax.set_title(f'#{i + 1}') data = DataExp2(pid) mpl.rcParams['font.size'] -= 2 h, l = data.plot_stacked_bar(ax, plot_legend=False) mpl.rcParams['font.size'] += 2 y_human, err = data.plot_line_human() ax.errorbar(DataExp2.x, y_human, err, label='Human $\pm$ sem', color=colors['decision_human'], capsize=3, capthick=1, lw=1, ms=2, fmt='o', zorder=3) m1 = data.load_model(models.ChoiceModel4Param, DataExp1(pid).build_model(models.ChoiceModel4Param).fit()) y1 = data.plot_line_model(m1.predict(m1.fit())) ax.plot(DataExp2.x, y1, 'o--', label='Transfer model', color=colors['decision_transfer'], lw=1, ms=2, zorder=2) y2 = data.plot_line_model(data.cross_validate(models.ChoiceModel4Param)) ax.plot(DataExp2.x, y2, 'o-', label='Fitted model', color=colors['decision_model'], lw=1, ms=2, zorder=2) handles, labels = ax.get_legend_handles_labels() h += handles[::-1] l += labels[::-1] ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.set_xlabel(' ', labelpad=18) plt.gcf().legend(h, l, loc='lower center', ncol=7, handler_map={ErrorbarContainer: HandlerErrorbar(yerr_size=0.35)}) plt.tight_layout()
def make_errorbar_legend(ax: Axes = None) -> None: """size adjusted legend for error bars, default does not work well with different linestyles""" if ax is None: ax = pyplot.gca() ax.legend( handlelength=5, handleheight=3, handler_map={ErrorbarContainer: HandlerErrorbar(xerr_size=0.9, yerr_size=0.9)}, )
def plot_3B(ax: plt.Axes): data = pool(DataExp2) data.plot_stacked_bar(ax) n = len(ExpConfig.glo_exp2) y_human, y1, y2 = np.zeros(n), np.zeros(n), np.zeros(n) for pid in DataExp2.pids: data = DataExp2(pid) y_human += data.plot_line_human()[0] m1 = data.load_model( models.ChoiceModel4Param, DataExp1(pid).build_model(models.ChoiceModel4Param).fit()) y1 += data.plot_line_model(m1.predict(m1.fit())) y2 += data.plot_line_model( data.cross_validate(models.ChoiceModel4Param)) y_human, y1, y2 = y_human / len(DataExp2.pids), y1 / len( DataExp2.pids), y2 / len(DataExp2.pids) err = [np.sqrt(p * (1 - p) / len(DataExp2.pids) / 20) for p in y_human] ax.errorbar(DataExp2.x, y_human, err, label='Human $\pm$ sem', color=colors['decision_human'], capsize=5, capthick=1, lw=1, ms=3, fmt='o', zorder=3) ax.plot(DataExp2.x, y1, 'o--', label='Transfer model', color=colors['decision_transfer'], lw=1, ms=3, zorder=2) ax.plot(DataExp2.x, y2, 'o-', label='Fitted model', color=colors['decision_model'], lw=1, ms=3, zorder=2) handles, labels = ax.get_legend_handles_labels() ax.legend(handles[::-1], labels[::-1], loc='upper right', handler_map={ErrorbarContainer: HandlerErrorbar(yerr_size=0.35)}) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) plt.tight_layout()
def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans): #from matplotlib.collections import LineCollection # call the parent class function artists = HandlerErrorbar.create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans) #Identify the artists. just so we know what each is #NOTE: The order here is different to that in ErrorbarContainer, so we re-order barlinecols, rest = partition(is_line, artists) barlinecols = tuple(barlinecols) *caplines, legline, legline_marker = rest xerr_size, yerr_size = self.get_err_size(legend, xdescent, ydescent, width, height, fontsize) #NOTE: second time calling this (already done in HandlerErrorbar.create_artists legline_marker.set_pickradius( xerr_size*1.25 ) legline_marker.set_picker( ErrorbarPicker() ) return [legline_marker, caplines, barlinecols, legline]
def plot_3C(ax: plt.Axes, bin_edges_human: np.ndarray = np.array( [-1000, -4.5, -3, -1.5, 0, 1.5, 3, 4.5, 1000]), bin_edges_model: np.ndarray = np.array( [-1000, -4.5, -3, -1.5, 0, 1.5, 3, 4.5, 1000])): Δ, p_human, p1, p2 = [], [], [], [] for pid in DataExp2.pids: data = DataExp2(pid) model = data.build_model(models.BayesianIdealObserver) df = model.predict(model.fit()) Δ += list(np.log(df['C']) - np.log(df['H'])) model = data.load_model( models.ChoiceModel4Param, DataExp1(pid).build_model(models.ChoiceModel4Param).fit()) p1 += list(model.predict(model.fit())['C']) p2 += list(data.cross_validate(models.ChoiceModel4Param)['C']) p_human += list((data.df['choice'] == 'C') * 1.0) df = pd.DataFrame({'Δ': Δ, 'p_human': p_human, 'p1': p1, 'p2': p2}) x_human, y_human, yerr_human, x_model, y1, yerr1, y2, yerr2 = [], [], [], [], [], [], [], [] df['bin'] = pd.cut(df['Δ'], bin_edges_human, labels=False) for i in range(len(bin_edges_human) - 1): _df = df[df['bin'] == i] x_human.append(_df['Δ'].mean()) y_human.append(_df['p_human'].mean()) yerr_human.append(_df['p_human'].sem()) df['bin'] = pd.cut(df['Δ'], bin_edges_model, labels=False) for i in range(len(bin_edges_model) - 1): _df = df[df['bin'] == i] x_model.append(_df['Δ'].mean()) y1.append(_df['p1'].mean()) yerr1.append(_df['p1'].sem()) y2.append(_df['p2'].mean()) yerr2.append(_df['p2'].sem()) ax.errorbar(x_human, y_human, yerr_human, label='Human $\pm$ sem', color=colors['decision_human'], fmt='.', capsize=2, ms=2, capthick=0.5, zorder=1) ax.plot(x_model, y1, '--', color=colors['decision_transfer'], label='Transfer model', ms=1, zorder=0) ax.plot(x_model, y2, '-', color=colors['decision_model'], label='Fitted model', ms=1, zorder=0) ax.set_xlabel(r'logit( $P_\mathregular{ideal}(S=C\,|\,\bf{X}$) )') ax.set_ylabel(r'$P$(choice=$C\,|\,\bf{X}$)') ax.set_ylim(0, 1) handles, labels = ax.get_legend_handles_labels() ax.legend(handles[::-1], labels[::-1], loc='lower right', handler_map={ErrorbarContainer: HandlerErrorbar(yerr_size=0.25)}) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) plt.tight_layout()
def __init__(self, xerr_size=0.5, yerr_size=None, marker_pad=0.3, numpoints=None, **kw): HandlerErrorbar.__init__(self, xerr_size, yerr_size, marker_pad, numpoints, **kw) self.eventson = True
# Plots for var, histos in variables.iteritems(): histos = histos[0] # drop metadata fig = Figure() # one figure per variable canvas = FigureCanvasPdf(fig) axes = fig.add_subplot(111) # row, col, id (121+j, when plotting both) axes.grid(axis='y') if title: if bkgeff: axes.set_title('Background rejection efficiency') else: axes.set_title('Signal selection efficiency') axes.set_ylim(0, 3.5) axes.set_ylabel('Efficiency (w/ offset)') axes.set_xlim(*variables[var][1]) axes.set_xlabel(variables[var][2]) axes.xaxis.set_label_coords(0.9,-0.05) for k, cut in enumerate(mva_cuts): if not histos[k].GetEntries(): continue x, y, yerr = th12errorbar(histos[k], yerr=True, asym=True) axes.errorbar(x, y, yerr=yerr, xerr=None, fmt='none', label='{}>{}'.format(classifier, cut)) axes.legend(fontsize=10, numpoints=1, frameon=False, ncol=ncuts, handler_map={mpl.lines.Line2D: HandlerErrorbar()}) if doprint: pp.savefig(fig) # pp.savefig() for current fig if doprint: pp.close()
def legend_darklight(axes, darklabel='Monte Carlo', lightlabel='Analytic', errtype=None, twosigma=False, extralabel=None): """Makes a legend for the ax object 'axes' at the location 'loc', which indicates dark solid lines as 'MC' (or darklabel) and light dotted lines as 'Analytic' (or lightlabel). Parameters ---------- axes : axes.Axes Description of parameter `axes`. darklabel : str Label of the dark objects in the legend. lightlabel : str Label of the light objects in the legend. errtype : str Specifies the plot type of the `dark` data, and thus the corresponding legend icon. None (default): legend for line plot. yerr: legend for a plot with ecolor modstyle: legend for a modstyle errorbar plot twosigma : bool Determines whether to include lighter, two sigma error bars in the legend. Returns ------- type Description of returned object. """ if errtype is None: custom_lines = [Line2D([0], [0], **style_solid, color=compcolors[(-1, 'dark')]), Line2D([0], [0], **style_dashed, color=compcolors[(-1, 'light')])] axes.legend(custom_lines, [darklabel, lightlabel]) elif errtype == 'yerr': _, xmax = axes.get_xlim() axes.errorbar(xmax*50., 0, yerr=1., **style_yerr, color='black', ecolor='gray', label=darklabel) if twosigma: axes.errorbar(xmax*50., 0, yerr=2., **style_yerr, color='black', ecolor='lightgray', label=darklabel) if extralabel is not None: axes.errorbar(xmax*50., 0, yerr=1., **style_yerr_ps, color=compcolors[(-1, 'medium')], ecolor=compcolors[(-1, 'light')], label=extralabel) if twosigma: axes.errorbar(xmax*50., 0, yerr=2., **style_yerr_ps, color=compcolors[(-1, 'medium')], ecolor=compcolors[(-1, 'light')], label=extralabel) handles, _ = axes.get_legend_handles_labels() if twosigma: l = 0 # Setting up containers for both errorbars if extralabel is not None: twosig_extra = container.ErrorbarContainer( handles[-1].lines, has_xerr=False, has_yerr=True) onesig_extra = container.ErrorbarContainer( handles[-2].lines, has_xerr=False, has_yerr=True) l = -2 twosig = container.ErrorbarContainer( handles[l-1].lines, has_xerr=False, has_yerr=True) # Setting up containers for both errorbars onesig = container.ErrorbarContainer( handles[l-2].lines, has_xerr=False, has_yerr=True) if extralabel is not None: custom_handles = [(twosig, onesig), (twosig_extra, onesig_extra)] else: custom_handles = [(twosig, onesig)] else: l = 0 # Setting up containers for both errorbars if extralabel is not None: onesig_extra = container.ErrorbarContainer( handles[-1].lines, has_xerr=False, has_yerr=True) l=-1 onesig = container.ErrorbarContainer( handles[l-1].lines, has_xerr=False, has_yerr=True) if extralabel is not None: custom_handles = [onesig, onesig_extra] else: custom_handles = [onesig] custom_handles.append( Line2D([0], [0], **style_dashed, color=compcolors[(-1, 'light')]) ) if twosigma: if extralabel is not None: axes.legend(custom_handles, [darklabel, extralabel, lightlabel], handler_map={ onesig: HandlerErrorbar(xerr_size=0.37), twosig: HandlerErrorbar(xerr_size=0.65), onesig_extra: HandlerErrorbar(xerr_size=0.37), twosig_extra: HandlerErrorbar(xerr_size=0.65)} ) else: axes.legend(custom_handles, [darklabel, lightlabel], handler_map={ onesig: HandlerErrorbar(xerr_size=0.37), twosig: HandlerErrorbar(xerr_size=0.65)} ) else: if extralabel is not None: axes.legend(custom_handles, [darklabel, extralabel, lightlabel]) else: axes.legend(custom_handles, [darklabel, lightlabel]) elif errtype == 'modstyle': _, xmax = axes.get_xlim() axes.errorbar(xmax*50., 0, yerr=1., **modstyle, color=compcolors[(-1, 'dark')], label=darklabel) if extralabel is not None: axes.errorbar(xmax*50., 0, yerr=1., **modstyle_ps, color=compcolors[(-1, 'medium')], label=extralabel) handles, _ = axes.get_legend_handles_labels() # Setting up containers for errorbars l = 0 if extralabel is not None: onesig_extra = container.ErrorbarContainer( handles[-1].lines, has_xerr=True, has_yerr=True) l=-1 onesig = container.ErrorbarContainer(handles[l-1].lines, has_xerr=True, has_yerr=True) if extralabel is not None: custom_handles = [onesig, onesig_extra] else: custom_handles = [onesig] custom_handles.append( Line2D([0], [0], **style_dashed, color=compcolors[(-1, 'light')]) ) if extralabel is not None: axes.legend(custom_handles, [darklabel, extralabel, lightlabel], prop={'size': 15}, loc='upper left') else: axes.legend(custom_handles, [darklabel, lightlabel])
# p9 = ax.plot([], [], color='tab:green', linewidth=0.5) # p10 = ax.fill([], [], color='tab:green', alpha=0.45) p0 = ax.errorbar([-3], [0], [2], fmt='o', color='black', capsize=1.2,ms=2.5,elinewidth=0.6,mew=0.6) # ax.legend() def make_legend_polygon(legend, orig_handle, xdescent, ydescent, width, height, fontsize): a=2*height p = mpatches.Polygon(np.array([[0,-a/2],[width,-a/2],[width,height+a/2],[0,height+a/2],[0,-a/2]])) return p hm = {p0: HandlerErrorbar(xerr_size=0.9), p4[0]: HandlerPatch(patch_func=make_legend_polygon), p2[0]: HandlerPatch(patch_func=make_legend_polygon), p6[0]: HandlerPatch(patch_func=make_legend_polygon),p8[0]: HandlerPatch(patch_func=make_legend_polygon)} l=ax.legend([(p3[0],p4[0]),(p5[0],p6[0]),(p7[0], p8[0]),(p1[0], p2[0]),p0], ['Gardner et al.'+'$^{5}$'+ ' (2003-2009):\ngravi., in-situ, elev. change','Wouters et al.'+'$^{19}$'+' (2002-2016):\ngravi.','Ciracì et al.'+'$^{20}$'+' (2002-2019):\ngravi.','Zemp et al.'+'$^{21}$'+' (2006-2015):\nin-situ, elev. change','This study\n(corresp. period):\nelev. change'],ncol=3, handlelength=0.75,framealpha=0.8,loc='upper right',labelspacing=0.25,handler_map=hm,borderpad=0.4) # ax.yaxis.grid(True,linestyle='--') ax.tick_params(width=0.35,length=2.5) l.get_frame().set_linewidth(0.5) reg_dir = '/home/atom/ongoing/work_worldwide/vol/final' list_fn_reg_multann = [os.path.join(reg_dir,'dh_'+str(i).zfill(2)+'_rgi60_int_base_reg_subperiods.csv') for i in np.arange(1,20)] df_all = pd.DataFrame() for fn_reg_multann in list_fn_reg_multann: df_all= df_all.append(pd.read_csv(fn_reg_multann)) tlims = [np.datetime64('20'+str(i).zfill(2)+'-01-01') for i in range(21)] list_df_glob = []
linestyle=':', capsize=cap_size, label='CLO w/ early prediction\n + Bayesian optimization') # plt.xticks(np.arange(max_budget+1)) return h h = plot_4c(ax5) ax5.set_xlim((ax5.get_xlim()[0], ax5.get_xlim()[1]+1)) ax5.set_ylim((-1100, 23500)) xrange = ax5.get_xlim()[1] - ax5.get_xlim()[0] yrange = ax5.get_ylim()[1] - ax5.get_ylim()[0] ax5.set_aspect(aspect=xrange/yrange) ax5.legend(frameon=False, loc='upper left', bbox_to_anchor=(-0.035, 1), handler_map={type(h): HandlerErrorbar(xerr_size=0.6)}) ax5.set_ylabel('Experimental time (hours)') ax5.set_xlabel('True cycle life of current best protocol\n(cycles)') # Log inset add_inset = False if add_inset: ax_ins = inset_axes(ax5, width='100%', height='100%', loc='upper left', bbox_to_anchor=(0.12,0.28,0.5,0.38), bbox_transform=ax5.transAxes) plot_4c(ax_ins) ax_ins.set_xlim((825,900)) ax_ins.set_ylim((100,30000)) ax_ins.set_yscale('symlog') # tight layout and save plt.tight_layout()