def plot_and_label_1d_signal_and_background_with_matplotlib_on_axis(ax: matplotlib.axes.Axes, jet_hadron: "correlations.Correlations", apply_correlation_scale_factor: bool = True) -> None: """ Plot and label the signal and background dominated hists on the given axis. This is a helper function so that we don't have to repeat code when we need to plot these hists. It can also be used in other modules. Args: ax: Axis on which the histograms should be plotted. jet_hadron: Correlations object from which the delta_phi hists should be retrieved. apply_correlation_scale_factor: Whether to scale the histogram by the correlation scale factor. Returns: None. The given axis is modified. """ # Setup hists = jet_hadron.correlation_hists_delta_phi h_signal = histogram.Histogram1D.from_existing_hist(hists.signal_dominated.hist) if apply_correlation_scale_factor: h_signal *= jet_hadron.correlation_scale_factor ax.errorbar( h_signal.x, h_signal.y, yerr = h_signal.errors, label = hists.signal_dominated.type.display_str(), marker = "o", linestyle = "", ) h_background = histogram.Histogram1D.from_existing_hist(hists.background_dominated.hist) if apply_correlation_scale_factor: h_background *= jet_hadron.correlation_scale_factor # Plot with opacity first background_plot = ax.errorbar( h_background.x, h_background.y, yerr = h_background.errors, marker = "o", linestyle = "", alpha = 0.5, ) # Then restrict range and plot without opacity near_side = len(h_background.x) // 2 ax.errorbar( h_background.x[:near_side], h_background.y[:near_side], yerr = h_background.errors[:near_side], label = hists.background_dominated.type.display_str(), marker = "o", linestyle = "", color = background_plot[0].get_color() ) # Set labels. ax.set_xlabel(labels.make_valid_latex_string(hists.signal_dominated.hist.GetXaxis().GetTitle())) ax.set_ylabel(labels.make_valid_latex_string(hists.signal_dominated.hist.GetYaxis().GetTitle())) jet_pt_label = labels.jet_pt_range_string(jet_hadron.jet_pt) track_pt_label = labels.track_pt_range_string(jet_hadron.track_pt) ax.set_title(fr"Unsubtracted 1D ${hists.signal_dominated.axis.display_str()}$," f" {jet_hadron.reaction_plane_orientation.display_str()} event plane orient.," f" {jet_pt_label}, {track_pt_label}")
def err_plot(x, y, yerr=None, xerr=None, axis: mpl.axes.Axes = None, **mpl_args): """Plot graph with error bars. Decided weather to plot points with error bars or lines with shaded areas depending on the number of plotted points. Parameters ---------- x, y : array_like x and y coordinates of the data to plot. yerr, xerr : array_like, optional The corresponding error of the data. Has same shape `x` and `y`. axis : mpl.axes.Axes, optional `mpl.axes.Axes` object used for plotting. mpl_args : Arguments for plotting passed to `axis.errorbar` or `axis.plot`. """ axis = plt.gca() if axis is None else axis x = np.asarray(x) if x.size > 50: # continuous plot try: ecolor = mpl_args.pop('ecolor') except KeyError: # no color defined -> try color else default ecolor = mpl_args.get('color', None) try: fmt = mpl_args.pop('fmt') except KeyError: baseline, = axis.plot(x, y, **mpl_args) else: baseline, = axis.plot(x, y, fmt, **mpl_args) if ecolor is None: ecolor = baseline.get_color() if yerr is not None: axis.fill_between(x, y-yerr, y+yerr, color=ecolor, alpha=.3, zorder=1) if xerr is not None: axis.fill_betweenx(y, x-xerr, x+xerr, color=ecolor, alpha=.3, zorder=1) else: default_args = {'capsize': 2.5, 'elinewidth': .3} default_args.update(mpl_args) axis.errorbar(x, y, yerr=yerr, **default_args)
def _reference_v2_a_data( reference_data: ReferenceData, ax: matplotlib.axes.Axes, selected_analysis_options: params.SelectedAnalysisOptions) -> None: # Determine the centrality key (because they don't map cleanly onto our centrality ranges) reference_data_centrality_map = { params.EventActivity.central: "0-5", params.EventActivity.semi_central: "30-40", } centrality_label = reference_data_centrality_map[ selected_analysis_options.event_activity] # Retrieve the data hist = reference_data["ptDependent"][centrality_label] # Plot and label it on the existing axis. ax.errorbar( hist.x, hist.y, yerr=hist.errors, marker="o", linestyle="", label=fr"ALICE {centrality_label}\% " + labels.make_valid_latex_string("v_{2}") + r"\{2, $| \Delta\eta |>1$\}", )
def plot_scatter(ax: mpl.axes.Axes, accuracy_samples: np.ndarray, ece_samples: np.ndarray, limit=5, plot_kwargs: Dict[str, Any] = {}) -> mpl.axes.Axes: _plot_kwargs = DEFAULT_PLOT_KWARGS.copy() _plot_kwargs.update(plot_kwargs) # plot x = np.mean(accuracy_samples, axis=1) y = np.mean(ece_samples, axis=1) xerr = np.std(accuracy_samples, axis=1) yerr = np.std(ece_samples, axis=1) # most accuracy top k idx = x.argsort()[-limit:][::-1] ax.errorbar(x[idx], y[idx], xerr=xerr[idx], yerr=yerr[idx], fmt='o', alpha=0.8, color='b', **_plot_kwargs) # least accuracy top k idx = x.argsort()[:limit] ax.errorbar(x[idx], y[idx], xerr=xerr[idx], yerr=yerr[idx], fmt='o', alpha=0.8, color='r', **_plot_kwargs) # other predicted classes idx = x.argsort()[limit:-limit] ax.errorbar(x[idx], y[idx], xerr=xerr[idx], yerr=yerr[idx], fmt='o', alpha=0.2, color='k', **_plot_kwargs) ax.set_xlabel('Accuracy') # ax.set_ylabel('ECE') return ax
def plot_reliability_comparison(ax: mpl.axes.Axes, N_list: List[int], bayesian_ece: np.ndarray, frequentist_ece: np.ndarray, bayesian_ece_std: np.ndarray, frequentist_ece_std: np.ndarray, ece_true: float, ylim: float, plot_kwargs: Dict[str, Any] = {}, plot_errorbar: bool = False) -> mpl.axes.Axes: """ Plot comparison of calibration estimation error obtained with the Bayesian or frequentist method. :param ax: :param N_list: List[int] :param bayesian_ece: (len(N_list), ) as type float. Estimation of ECE obtained with the Bayesian method. :param frequentist_ece: (len(N_list), ) as type float. Estimation of ECE obtained with the frequentist method. :param bayesian_ece_std: (len(N_list), ) as type float. Standard deviation of estimation of ECE obtained with the Bayesian method. :param frequentist_ece_std: (len(N_list), ) as type float. Standard deviation of estimation of ECE obtained with the frequentist method. :param ece_true: float. Ground truth ECE :param plot_kwargs: dict. Keyword arguments passed to the plot. :return: ax: the generated matplotlib ax """ _plot_kwargs = DEFAULT_PLOT_KWARGS.copy() _plot_kwargs.update(plot_kwargs) if plot_errorbar: # todo: debug ax.errorbar(N_list, (bayesian_ece - ece_true) / ece_true * 100, bayesian_ece_std / ece_true * 100, '-*', **_plot_kwargs, label='Bayesian', color='tab:red') ax.errorbar(N_list, (frequentist_ece - ece_true) / ece_true * 100, frequentist_ece_std / ece_true * 100, '-o', **_plot_kwargs, label='Frequentist', color='tab:blue') else: ax.plot(N_list, (bayesian_ece - ece_true) / ece_true * 100, '-*', **_plot_kwargs, label='Bayesian', color='tab:red') ax.plot(N_list, (frequentist_ece - ece_true) / ece_true * 100, '-o', **_plot_kwargs, label='Frequentist', color='tab:blue') ax.set_xscale('log') ax.set_xlabel('#queries', labelpad=0.2) ax.xaxis.set_ticks(N_list) ax.set_ylim(ymin=0, ymax=ylim) ax.set_yticks((0, ylim / 2, ylim)) ax.yaxis.set_major_formatter(PercentFormatter(decimals=0)) ax.tick_params(pad=0.25, length=1.5) return ax