def plot_cap(y, y_pred, title=None, xlabel=None, ylabel=None,
             savefig=False, fname=None, **kwargs):
    """Plot Cumulative Accuracy Profile (CAP).

    Parameters
    ----------
    y : array-like, shape = (n_samples,)
        Array with the target labels.

    y_pred : array-like, shape = (n_samples,)
        Array with predicted probabilities.

    title : str or None, optional (default=None)
        Title for the plot.

    xlabel : str or None, optional (default=None)
        Label for the x-axis.

    ylabel : str or None, optional (default=None)
        Label for the y-axis.

    savefig : bool (default=False)
        Whether to save the figure.

    fname : str or None, optional (default=None)
        Name for the figure file.

    **kwargs : keyword arguments
        Keyword arguments for matplotlib.pyplot.savefig().
    """
    y, y_pred = _check_arrays(y, y_pred)

    _check_parameters(title, xlabel, ylabel, savefig, fname)

    n_samples = y.shape[0]
    n_event = np.sum(y)

    idx = y_pred.argsort()[::-1][:n_samples]
    yy = y[idx]

    p_event = np.append([0], np.cumsum(yy)) / n_event
    p_population = np.arange(0, n_samples + 1) / n_samples

    auroc = roc_auc_score(y, y_pred)
    gini = auroc * 2 - 1

    # Define the plot settings
    if title is None:
        title = "Cumulative Accuracy Profile (CAP)"
    if xlabel is None:
        xlabel = "Fraction of all population"
    if ylabel is None:
        ylabel = "Fraction of event population"

    plt.plot([0, 1], [0, 1], color='k', linestyle='--', label="Random Model")
    plt.plot([0, n_event / n_samples, 1], [0, 1, 1], color='grey',
             linestyle='--', label="Perfect Model")
    plt.plot(p_population, p_event, color="g",
             label="Model (Gini: {:.5f})".format(gini))

    plt.title(title, fontdict={'fontsize': 14})
    plt.xlabel(xlabel, fontdict={'fontsize': 12})
    plt.ylabel(ylabel, fontdict={'fontsize': 12})
    plt.legend(loc='lower right')

    # Save figure if requested. Pass kwargs.
    if savefig:
        plt.savefig(fname=fname, **kwargs)
        plt.close()
Plot Cumulative Accuracy Profile (CAP).

    Parameters
    ----------
    y : array-like, shape = (n_samples,)
        Array with the target labels.

    y_pred : array-like, shape = (n_samples,)
        Array with predicted probabilities.

    title : str or None, optional (default=None)
        Title for the plot.

    xlabel : str or None, optional (default=None)
        Label for the x-axis.

    ylabel : str or None, optional (default=None)
        Label for the y-axis.

    savefig : bool (default=False)
        Whether to save the figure.

    fname : str or None, optional (default=None)
        Name for the figure file.

    **kwargs : keyword arguments
        Keyword arguments for matplotlib.pyplot.savefig().
    
optbinning/scorecard/plots.py