jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def plot_results(data, keys, saver, x='losses', hue='correct', col='noise', kind='box', style='whitegrid', title=None):
    sns.set_style(style)
    n = len(keys)

    for k in keys:
        g = sns.factorplot(x=x, y=k, hue=hue, col=col, data=data, kind=kind)
        g.set(ylim=(0, 1))
        if title is not None:
            g.fig.subplots_adjust(top=0.9)
            g.fig.suptitle('{} - {}'.format(k, title))
        saver.save_fig(g.fig, '{}_{}'.format(kind, k))
//  [python]
def plot_results(data, keys, saver, x='losses', hue='correct', col='noise', kind='box', style='whitegrid', title=None):
    sns.set_style(style)
    n = len(keys)

    for k in keys:
        g = sns.catplot(x=x, y=k, hue=hue, col=col, data=data, kind=kind)
        g.set(ylim=(0, 1))
        if title is not None:
            g.fig.subplots_adjust(top=0.9)
            g.fig.suptitle('{} - {}'.format(k, title))
        saver.save_fig(g.fig, '{}_{}'.format(kind, k))
//  [python]
def plot_vh_inequalities_empirical_summary(df_rank, x='mae', fn=None):

    if x not in ['mae','me']:
        raise Exception('invalid x-axis (horizontal ineq.)')
        
    ### only man data points
    tmp = df_rank.groupby(['dataset','kind','metric']).mean().reset_index()
    tmp.drop(columns=['rank', 'fmt'], inplace=True)

    ### main plot
    tmp.sort_values(by=["dataset","metric"], inplace=True)
    fg = sns.factorplot(data=tmp,
                     x=x, y='gini',
                     height=2.0,aspect=0.9,
                     hue='metric', col='dataset')
    [plt.setp(ax.texts, text="") for ax in fg.axes.flat]
    fg.set_titles(row_template='{row_name}', col_template='{col_name}')

    ### labels and xticks
    for ax in fg.axes.flatten():
        ax.set_xlabel("")
        ax.set_ylabel("")

        # xticks
        #ax.xaxis.set_major_locator(plt.MaxNLocator(3))
        #ax.xaxis.set_minor_locator(plt.MaxNLocator(10))

        # xticklabels
        xtls = ax.get_xticklabels()
        ax.set_xticklabels([round(float(xtl.get_text()), 2) for i, xtl in enumerate(xtls)], rotation=0)
        
        #ax.axvline(x=0 if x=='me' else 0.5, ls='--', c='black', lw=0.5)
        #ax.axhline(y=0.5, ls='--', c='black', lw=0.5)
        
    ### ylabel
    ylabel = 'Gini'
    if fg.axes.shape[0] % 2 != 0:
        fg.axes[int(fg.axes.shape[0] / 2), 0].set_ylabel(ylabel)
    else:
        fg.axes[int(fg.axes.shape[0] / 2), 0].text(-50, 0.28, ylabel, {'ha': 'center', 'va': 'center'}, rotation=90)

    ### xlabel
    xlabel = x.upper() #'MAE of fraction of minorities in top-k%'
    if fg.axes.shape[1] % 2 != 0:
        fg.axes[-1, int(fg.axes.shape[1] / 2)].set_xlabel(xlabel)
    else:
        fg.axes[-1, int(fg.axes.shape[1] / 2)].text(0, 0.15, xlabel, {'ha': 'center', 'va': 'center'}, rotation=0)

    ### space between subplots
    plt.subplots_adjust(hspace=0.1, wspace=0.1)

    ### Save fig
    if fn is not None:
        fg.savefig(fn, bbox_inches='tight')
        print('{} saved!'.format(fn))
//  [python]
def plot_vh_inequalities_empirical_summary(df_rank, x='mae', fn=None):

    if x not in ['mae','me']:
        raise Exception('invalid x-axis (horizontal ineq.)')
        
    ### only man data points
    tmp = df_rank.groupby(['dataset','kind','metric']).mean().reset_index()
    tmp.drop(columns=['rank', 'fmt'], inplace=True)

    ### main plot
    tmp.sort_values(by=["dataset","metric"], inplace=True)
    fg = sns.catplot(data=tmp,
                     x=x, y='gini',
                     height=2.0,aspect=0.9,
                     hue='metric', col='dataset')
    [plt.setp(ax.texts, text="") for ax in fg.axes.flat]
    fg.set_titles(row_template='{row_name}', col_template='{col_name}')

    ### labels and xticks
    for ax in fg.axes.flatten():
        ax.set_xlabel("")
        ax.set_ylabel("")

        # xticks
        #ax.xaxis.set_major_locator(plt.MaxNLocator(3))
        #ax.xaxis.set_minor_locator(plt.MaxNLocator(10))

        # xticklabels
        xtls = ax.get_xticklabels()
        ax.set_xticklabels([round(float(xtl.get_text()), 2) for i, xtl in enumerate(xtls)], rotation=0)
        
        #ax.axvline(x=0 if x=='me' else 0.5, ls='--', c='black', lw=0.5)
        #ax.axhline(y=0.5, ls='--', c='black', lw=0.5)
        
    ### ylabel
    ylabel = 'Gini'
    if fg.axes.shape[0] % 2 != 0:
        fg.axes[int(fg.axes.shape[0] / 2), 0].set_ylabel(ylabel)
    else:
        fg.axes[int(fg.axes.shape[0] / 2), 0].text(-50, 0.28, ylabel, {'ha': 'center', 'va': 'center'}, rotation=90)

    ### xlabel
    xlabel = x.upper() #'MAE of fraction of minorities in top-k%'
    if fg.axes.shape[1] % 2 != 0:
        fg.axes[-1, int(fg.axes.shape[1] / 2)].set_xlabel(xlabel)
    else:
        fg.axes[-1, int(fg.axes.shape[1] / 2)].text(0, 0.15, xlabel, {'ha': 'center', 'va': 'center'}, rotation=0)

    ### space between subplots
    plt.subplots_adjust(hspace=0.1, wspace=0.1)

    ### Save fig
    if fn is not None:
        fg.savefig(fn, bbox_inches='tight')
        print('{} saved!'.format(fn))
//  [python]
def plot_violin(plot_df: pd.DataFrame, palette) -> sns.axisgrid.FacetGrid:
    describe = plot_df.groupby("Model").describe()
    logger.info(describe)
    # violin plot
    mean_order = (
        plot_df.groupby("Model")["value"]
        .mean()
        .reset_index()
        .sort_values("value", ascending=False)["Model"]
    )
    logger.info(mean_order)
    ax = sns.violinplot(
        x="Model",
        y="value",
        data=plot_df,
        order=mean_order,
        palette=palette,
    )
    ax.set(xlabel="Model/Method", ylabel="Weighted average of top 10 batet scores")
    ax.set_xticklabels(rotation=45, ha="right")
    return ax
//  [python]
def plot_violin(plot_df: pd.DataFrame, palette) -> sns.axisgrid.FacetGrid:
    describe = plot_df.groupby("Model").describe()
    logger.info(describe)
    # violin plot
    mean_order = (
        plot_df.groupby("Model")["value"]
        .mean()
        .reset_index()
        .sort_values("value", ascending=False)["Model"]
    )
    logger.info(mean_order)
    ax = sns.catplot(
        x="Model",
        y="value",
        data=plot_df,
        kind="violin",
        order=mean_order,
        palette=palette,
    )
    ax.set(xlabel="Model/Method", ylabel="Weighted average of top 10 batet scores")
    ax.set_xticklabels(rotation=45, ha="right")
    return ax
//  [python]
def plot_synthetic_rankings(df_rank, model=None, metric='pagerank', y='fmt', sym=True, fn=None):
    
    if y not in ['fmt','gt']:
        raise Exception('Invalid x-axis (horizontal ineq.)')
        
    col = 'fm'
    row = 'hmm'
    
    fm = [0.1, 0.3, 0.5]
    data = df_rank.query("metric == @metric & fm in @fm").copy()
    if model is not None:
        data = data.query("kind == @model")
    
    ### Type of homophily: symmetric or not
    if sym:
        data = data.query("hmm == hMM").copy()
        colors = BrBG_11.mpl_colors
        colors[int(len(colors)/2)] = 'lightgrey'
        fg = sns.factorplot(data=data,
                         col=col,
                         hue='hMM',
                         x='rank', y=y,
                         kind='point',
                         sharey=True,
                         size=2.5, aspect=1,
                         legend=True,
                         legend_out=True,
                         palette=colors
                         )
    else:
        hm = [0.2, 0.5, 0.8]
        hM = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
        palette = BrBG_11 if len(hM)==11 else BrBG_5
        colors = palette.mpl_colors
        colors[int(len(colors)/2)] = 'lightgrey'
        data = data.query("hmm in @hm and hMM in @hM").copy()
        fg = sns.factorplot(data=data,
                         col=col,
                         row=row,
                         hue='hMM',
                         margin_titles=True,
                         x='rank', y=y,
                         kind='point',
                         sharey=True,
                         size=2, aspect=1,
                         legend=True,
                         legend_out=True,
                         palette=colors)

    ### baseline: fm
    ncol = data[col].nunique()
    nrow = 1 if sym else data[row].nunique()
    for i, ax in enumerate(fg.axes.flatten()):
        # labels
        ax.set_xlabel('')
        ax.set_ylabel('')

        # xticks
        ax.set_xticklabels([int(float(xtl.get_text())) 
                            if int(float(xtl.get_text())) in [10, 50, 90] else '' for xtl in ax.get_xticklabels()], 
                           rotation=0)

        # baseline
        try:
            r = int(i / ncol)
            c = i - (r * ncol)
            
            if y == 'fmt':
                fm = float(fg.axes[0,c].get_title().replace("fm = ","").replace(" ",""))
            else:
                fm = 0
            ax.axhline(fm, c='black', ls='--', lw=2.0, zorder=1000)
        except:
            pass

    ### labels
    fg.axes[-1,int(ncol/2)].set_xlabel('Top-k rank %')
    ylabel = 'Fraction of minorities' if y == 'fmt' else 'Gini'
    fg.axes[int(fg.axes.shape[0]/2),0].set_ylabel('{} in Top-k rank %'.format(ylabel))

    ### legend
    if sym:
        fg._legend.set_title("h")

    ### space between subplots
    plt.subplots_adjust(hspace=0.1, wspace=0.1)

    ### Save fig
    if fn is not None:
        fg.savefig(fn, bbox_inches='tight')
        print('{} saved!'.format(fn))
        
    plt.show()
    plt.close()
//  [python]
def plot_synthetic_rankings(df_rank, model=None, metric='pagerank', y='fmt', sym=True, fn=None):
    
    if y not in ['fmt','gt']:
        raise Exception('Invalid x-axis (horizontal ineq.)')
        
    col = 'fm'
    row = 'hmm'
    
    fm = [0.1, 0.3, 0.5]
    data = df_rank.query("metric == @metric & fm in @fm").copy()
    if model is not None:
        data = data.query("kind == @model")
    
    ### Type of homophily: symmetric or not
    if sym:
        data = data.query("hmm == hMM").copy()
        colors = BrBG_11.mpl_colors
        colors[int(len(colors)/2)] = 'lightgrey'
        fg = sns.catplot(data=data,
                         col=col,
                         hue='hMM',
                         x='rank', y=y,
                         kind='point',
                         sharey=True,
                         height=2.5, aspect=1,
                         legend=True,
                         legend_out=True,
                         palette=colors
                         )
    else:
        hm = [0.2, 0.5, 0.8]
        hM = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
        palette = BrBG_11 if len(hM)==11 else BrBG_5
        colors = palette.mpl_colors
        colors[int(len(colors)/2)] = 'lightgrey'
        data = data.query("hmm in @hm and hMM in @hM").copy()
        fg = sns.catplot(data=data,
                         col=col,
                         row=row,
                         hue='hMM',
                         margin_titles=True,
                         x='rank', y=y,
                         kind='point',
                         sharey=True,
                         height=2, aspect=1,
                         legend=True,
                         legend_out=True,
                         palette=colors)

    ### baseline: fm
    ncol = data[col].nunique()
    nrow = 1 if sym else data[row].nunique()
    for i, ax in enumerate(fg.axes.flatten()):
        # labels
        ax.set_xlabel('')
        ax.set_ylabel('')

        # xticks
        ax.set_xticklabels([int(float(xtl.get_text())) 
                            if int(float(xtl.get_text())) in [10, 50, 90] else '' for xtl in ax.get_xticklabels()], 
                           rotation=0)

        # baseline
        try:
            r = int(i / ncol)
            c = i - (r * ncol)
            
            if y == 'fmt':
                fm = float(fg.axes[0,c].get_title().replace("fm = ","").replace(" ",""))
            else:
                fm = 0
            ax.axhline(fm, c='black', ls='--', lw=2.0, zorder=1000)
        except:
            pass

    ### labels
    fg.axes[-1,int(ncol/2)].set_xlabel('Top-k rank %')
    ylabel = 'Fraction of minorities' if y == 'fmt' else 'Gini'
    fg.axes[int(fg.axes.shape[0]/2),0].set_ylabel('{} in Top-k rank %'.format(ylabel))

    ### legend
    if sym:
        fg._legend.set_title("h")

    ### space between subplots
    plt.subplots_adjust(hspace=0.1, wspace=0.1)

    ### Save fig
    if fn is not None:
        fg.savefig(fn, bbox_inches='tight')
        print('{} saved!'.format(fn))
        
    plt.show()
    plt.close()
//  [python]
def plot_fig1(df):
    algs_order = ["postgres", "mscn", "mscn-priority", "microsoft",
        "microsoft-priority", "true"]
    df = df[df["samples_type"] == "test"]
    df = pd.melt(df, value_vars=["runtime", "cost"], id_vars="alg")
    # print(set(df["alg"]))

    plot_order = ["cost", "runtime"]
    fg = sns.factorplot(x = "alg",
            y = "value",
            # row = "variable",
            col = "variable",
            data = df,
            hue = "alg", order = algs_order, hue_order = algs_order,
            sharey = False,
            # kind="bar",
            # kind="box",
            kind="boxen",
            # row_order=plot_order,
            col_order=plot_order,
            ci = None,
            legend = False,
            # legend_out = True
            )

    fg.set(yscale="log")
    fg.set(ylabel="")


    for i, ax in enumerate(fg.axes.flatten()):
        if i == 0:
            # ax.legend(loc="upper left")
            # ax.legend(bbox_to_anchor=(0.0,1.0), loc="upper left")
            ax.set_ylabel('cost', fontsize=8)
            ax.set_title("Query Costs")
        else:
            ax.set_ylabel('seconds', fontsize=8)
            ax.set_title("Query Runtimes")

        for tick in ax.get_xticklabels():
            tick.set_rotation(45)

    # fg.add_legend()
    # plt.legend(loc='upper left')
    # plt.legend(bbox_to_anchor=(1.05, 1), loc="upper left", borderaxespad=0.)
    # fg.add_legend()

    # plt.xticks(rotation=45)
    plt.tight_layout(rect=[0, 0, 1, 0.95])

    plt.savefig("runtime_fig.png")

    plt.clf()
//  [python]
def plot_fig1(df):
    algs_order = ["postgres", "mscn", "mscn-priority", "microsoft",
        "microsoft-priority", "true"]
    df = df[df["samples_type"] == "test"]
    df = pd.melt(df, value_vars=["runtime", "cost"], id_vars="alg")
    # print(set(df["alg"]))

    plot_order = ["cost", "runtime"]
    fg = sns.catplot(x = "alg",
            y = "value",
            # row = "variable",
            col = "variable",
            data = df,
            hue = "alg", order = algs_order, hue_order = algs_order,
            sharey = False,
            # kind="bar",
            # kind="box",
            kind="boxen",
            # row_order=plot_order,
            col_order=plot_order,
            ci = None,
            legend = False,
            # legend_out = True
            )

    fg.set(yscale="log")
    fg.set(ylabel="")


    for i, ax in enumerate(fg.axes.flatten()):
        if i == 0:
            # ax.legend(loc="upper left")
            # ax.legend(bbox_to_anchor=(0.0,1.0), loc="upper left")
            ax.set_ylabel('cost', fontsize=8)
            ax.set_title("Query Costs")
        else:
            ax.set_ylabel('seconds', fontsize=8)
            ax.set_title("Query Runtimes")

        for tick in ax.get_xticklabels():
            tick.set_rotation(45)

    # fg.add_legend()
    # plt.legend(loc='upper left')
    # plt.legend(bbox_to_anchor=(1.05, 1), loc="upper left", borderaxespad=0.)
    # fg.add_legend()

    # plt.xticks(rotation=45)
    plt.tight_layout(rect=[0, 0, 1, 0.95])

    plt.savefig("runtime_fig.png")

    plt.clf()
//  [python]
def make_plot(df, out_filepath: str, palette: str = "dark", plot_type: str = "finetune") -> None: # from this example: https://seaborn.pydata.org/examples/grouped_barplot.html
    if plot_type == "finetune":
        order = ["GPT-2", "GPT-neo 1.3B", "GPT-3 Ada", "GPT-3 Babbage", "GPT-3 Curie", "GPT-3 Davinci", "BERT", "RoBERTa", "Human", "Human (confident)"]
    elif plot_type == "prompt":
        order = ["GPT-2", "GPT-neo 1.3B", "GPT-3 Ada"]
    elif plot_type == "transfer":
        order = ["BERT", "RoBERTa"]

    g = sns.factorplot(
    data=df, kind="bar",
    x="Model", y="Accuracy", hue="Method",
    ci="sd", palette=palette, size=6, order=order, col_order=["Zero-shot", "Transfer", "Fine-tuning", "Human"]
    )
    g.despine(left=True)
    g.set_axis_labels("", "Accuracy")
    g._legend.remove()

    plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
          ncol=3, fancybox=True, shadow=True)
    plt.ylim(0.5, 1)
    if plot_type != "prompt":
        plt.xticks(rotation=90)
    plt.tight_layout()
    plt.savefig(f"{out_filepath}.png")
    plt.savefig(f"{out_filepath}.eps")
//  [python]
def make_plot(df, out_filepath: str, palette: str = "dark", plot_type: str = "finetune") -> None: # from this example: https://seaborn.pydata.org/examples/grouped_barplot.html
    if plot_type == "finetune":
        order = ["GPT-2", "GPT-neo 1.3B", "GPT-3 Ada", "GPT-3 Babbage", "GPT-3 Curie", "GPT-3 Davinci", "BERT", "RoBERTa", "Human", "Human (confident)"]
    elif plot_type == "prompt":
        order = ["GPT-2", "GPT-neo 1.3B", "GPT-3 Ada"]
    elif plot_type == "transfer":
        order = ["BERT", "RoBERTa"]

    g = sns.catplot(
    data=df, kind="bar",
    x="Model", y="Accuracy", hue="Method",
    ci="sd", palette=palette, height=6, order=order, col_order=["Zero-shot", "Transfer", "Fine-tuning", "Human"]
    )
    g.despine(left=True)
    g.set_axis_labels("", "Accuracy")
    g._legend.remove()

    plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
          ncol=3, fancybox=True, shadow=True)
    plt.ylim(0.5, 1)
    if plot_type != "prompt":
        plt.xticks(rotation=90)
    plt.tight_layout()
    plt.savefig(f"{out_filepath}.png")
    plt.savefig(f"{out_filepath}.eps")
//  [python]
def plot_probability(data: pd.DataFrame, metric: str, 
                     model_name: Optional[str] = None,
                     models_to_remove: Optional[List[str]] = None,
                     bar_plot: bool = False, 
                     box_plot: bool = True,
                     cat_plot=False,
                     n_boot=100000,
                     **plot_kwargs):
    '''
    This assumes a dataframe that has the following columns:
    
    1. Metric
    2. Score
    3. Model
    4. Probability
    '''

    if model_name is not None:
        data = data[data['Model'] == model_name]
    data = data[data['Metric'] == metric]
    
    if not cat_plot:
        fig, ax = plt.subplots(1,1, **plot_kwargs)
    if box_plot:
        sns.boxplot(x='Probability', y='Score', hue='Model', 
                    data=data, ax=ax)
    elif bar_plot:
        if model_name is None:
            sns.barplot(x='Probability', y='Score', hue='Model', n_boot=n_boot,
                         data=data, ax=ax, ci=95)
        else:
            sns.barplot(x='Probability', y='Score', n_boot=n_boot,
                        data=data, ax=ax, ci=95)
    else:
        g = sns.FacetGrid(data, col="Model", col_wrap=3)
        g.map(sns.barplot, "Probability", "Score", n_boot=n_boot)
        g.set(ylim=(min_score, max_score))
        ax = g
        
    min_score = data['Score'].min()
    min_score = min_score - (min_score / 100)
    max_score = data['Score'].max()
    max_score = max_score + (max_score / 100)
    if not cat_plot:
        ax.set_ylim([min_score, max_score])
    else:
        ax.set(ylim=(min_score, max_score))
    return ax
//  [python]
def plot_probability(data: pd.DataFrame, metric: str, 
                     model_name: Optional[str] = None,
                     models_to_remove: Optional[List[str]] = None,
                     bar_plot: bool = False, 
                     box_plot: bool = True,
                     cat_plot=False,
                     n_boot=100000,
                     **plot_kwargs):
    '''
    This assumes a dataframe that has the following columns:
    
    1. Metric
    2. Score
    3. Model
    4. Probability
    '''

    if model_name is not None:
        data = data[data['Model'] == model_name]
    data = data[data['Metric'] == metric]
    
    if not cat_plot:
        fig, ax = plt.subplots(1,1, **plot_kwargs)
    if box_plot:
        sns.boxplot(x='Probability', y='Score', hue='Model', 
                    data=data, ax=ax)
    elif bar_plot:
        if model_name is None:
            sns.barplot(x='Probability', y='Score', hue='Model', n_boot=n_boot,
                         data=data, ax=ax, ci=95)
        else:
            sns.barplot(x='Probability', y='Score', n_boot=n_boot,
                        data=data, ax=ax, ci=95)
    else:
        ax = sns.catplot(x='Probability', y='Score',  
                    col='Model', data=data, kind='bar',
                    n_boot=n_boot,
                    col_wrap=3, facet_kws=plot_kwargs)
        
    min_score = data['Score'].min()
    min_score = min_score - (min_score / 100)
    max_score = data['Score'].max()
    max_score = max_score + (max_score / 100)
    if not cat_plot:
        ax.set_ylim([min_score, max_score])
    else:
        ax.set(ylim=(min_score, max_score))
    return ax
//  [python]
def cat6_relative_plot(df, divide_category=False, cat_name=None, y=1.):
    '''plot the 6 categories, without information from gnomAD or SSC; can be
    added later as ranges/lines etc. use catplot from sns and the four
    as defined in the figure. use these colors as indicated below.'''
    
    #generates table with all the categories, so nothing is left out
    df = make_table_TRIs_CATs(df, divide_category, cat_name)
    #removes all the TRI_CAT counts   
    df = df[df.TRI_CAT == '6_CAT']
    
    if divide_category == True:
        df['SUM'] = df.groupby(cat_name)['POS'].transform('sum')
    else:
        df['SUM'] = df['POS'].sum()

    df['REL_COUNT'] = df.POS/df.SUM
    
    colors=['g', 'xkcd:brown', 'xkcd:golden', 'xkcd:orangish red']
    
    sns.barplot(x='CAT', y='REL_COUNT', hue=cat_name, data=df,
                    palette=colors)
    
    plt.xlabel('Categories')
    plt.ylabel('Relative Contribution')
    plt.ylim(0,y)
    sns.despine(offset=5, trim=True)
    plt.show()
//  [python]
def cat6_relative_plot(df, divide_category=False, cat_name=None, y=1.):
    '''plot the 6 categories, without information from gnomAD or SSC; can be
    added later as ranges/lines etc. use catplot from sns and the four
    as defined in the figure. use these colors as indicated below.'''
    
    #generates table with all the categories, so nothing is left out
    df = make_table_TRIs_CATs(df, divide_category, cat_name)
    #removes all the TRI_CAT counts   
    df = df[df.TRI_CAT == '6_CAT']
    
    if divide_category == True:
        df['SUM'] = df.groupby(cat_name)['POS'].transform('sum')
    else:
        df['SUM'] = df['POS'].sum()

    df['REL_COUNT'] = df.POS/df.SUM
    
    colors=['g', 'xkcd:brown', 'xkcd:golden', 'xkcd:orangish red']
    
    sns.catplot(x='CAT', y='REL_COUNT', hue=cat_name, data=df,
                    kind='bar', palette=colors)
    
    plt.xlabel('Categories')
    plt.ylabel('Relative Contribution')
    plt.ylim(0,y)
    sns.despine(offset=5, trim=True)
    plt.show()
//  [python]
def tg_plot_huebars(df):
    
    df = df[(df.MUTSIG_CLASS == '1_sperm') & (df.INDEL == False)]
    
    df['BINS'] = pd.cut(df.MAF_SPERM_A, bins=[0., 0.02, 0.03, 0.05, 0.2])
    
    df = df.groupby(['BINS', 'CAT']).count()
    
    df['SUM'] = df.groupby('BINS')['POS'].transform('sum')
    df['REL_COUNT'] = df.POS/df.SUM
    df = df.reset_index()
    #df = df[df.CAT == 'T>G']
    
    g = sns.factorplot(x='CAT', y='REL_COUNT', hue='BINS', data=df, kind='bar')
    plt.show()
//  [python]
def tg_plot_huebars(df):
    
    df = df[(df.MUTSIG_CLASS == '1_sperm') & (df.INDEL == False)]
    
    df['BINS'] = pd.cut(df.MAF_SPERM_A, bins=[0., 0.02, 0.03, 0.05, 0.2])
    
    df = df.groupby(['BINS', 'CAT']).count()
    
    df['SUM'] = df.groupby('BINS')['POS'].transform('sum')
    df['REL_COUNT'] = df.POS/df.SUM
    df = df.reset_index()
    #df = df[df.CAT == 'T>G']
    
    sns.catplot(x='CAT', y='REL_COUNT', hue='BINS', data=df, kind='bar')
    plt.show()
//  [python]
def cat6_relative_plot_SSCgno(df, ssc_gno, y=1., suffix='_ssc'):
    '''plot the 6 categories, with information from gnomAD or SSC; use catplot
    from sns and the four as defined in the figure. use these colors as
    indicated below. gnomAD ssc is provided as a df generated as indicated
    above. suffix determines whether plot is for gno or ssc. default is ssc,
    but both plots will be needed.'''
    
    #generates table with all the categories, so nothing is left out
    df = make_table_TRIs_CATs(df, True, 'MUTSIG_CLASS')
    #removes all the TRI_CAT counts   
    df = df[df.TRI_CAT == '6_CAT']
    
    df['SUM'] = df.groupby('MUTSIG_CLASS')['POS'].transform('sum')

    df['REL_COUNT'] = df.POS/df.SUM
    
    df_merge = pd.merge(df, ssc_gno)
    
    colors=['g', 'xkcd:brown', 'xkcd:golden', 'xkcd:orangish red']
    
    ranges = make_ranges_MUTSIG_CLASS()
    
    sns.barplot(x='CAT', y='REL_COUNT', hue='MUTSIG_CLASS', data=df_merge,
                    palette=colors)
    
    df_vlines = df_merge.sort_values(by=['CAT', 'MUTSIG_CLASS'])
    
    plt.vlines(ranges, ymin=df_vlines['2.5%'+suffix],
                       ymax=df_vlines['97.5%'+suffix],
               alpha=0.5, linewidths=5, colors='0.8')
    
    plt.xlabel('Categories')
    plt.ylabel('Relative Contribution')
    plt.ylim(0,y)
    sns.despine(offset=5, trim=True)
    plt.show()
//  [python]
def cat6_relative_plot_SSCgno(df, ssc_gno, y=1., suffix='_ssc'):
    '''plot the 6 categories, with information from gnomAD or SSC; use catplot
    from sns and the four as defined in the figure. use these colors as
    indicated below. gnomAD ssc is provided as a df generated as indicated
    above. suffix determines whether plot is for gno or ssc. default is ssc,
    but both plots will be needed.'''
    
    #generates table with all the categories, so nothing is left out
    df = make_table_TRIs_CATs(df, True, 'MUTSIG_CLASS')
    #removes all the TRI_CAT counts   
    df = df[df.TRI_CAT == '6_CAT']
    
    df['SUM'] = df.groupby('MUTSIG_CLASS')['POS'].transform('sum')

    df['REL_COUNT'] = df.POS/df.SUM
    
    df_merge = pd.merge(df, ssc_gno)
    
    colors=['g', 'xkcd:brown', 'xkcd:golden', 'xkcd:orangish red']
    
    ranges = make_ranges_MUTSIG_CLASS()
    
    sns.catplot(x='CAT', y='REL_COUNT', hue='MUTSIG_CLASS', data=df_merge,
                    kind='bar', palette=colors)
    
    df_vlines = df_merge.sort_values(by=['CAT', 'MUTSIG_CLASS'])
    
    plt.vlines(ranges, ymin=df_vlines['2.5%'+suffix],
                       ymax=df_vlines['97.5%'+suffix],
               alpha=0.5, linewidths=5, colors='0.8')
    
    plt.xlabel('Categories')
    plt.ylabel('Relative Contribution')
    plt.ylim(0,y)
    sns.despine(offset=5, trim=True)
    plt.show()
//  [python]
def plot_x_error(df, x, y, xlabel, ylabel, title, outfile):

    sns.set(style="whitegrid")

    ax = sns.barplot(x=x, y=y, data=df)
    ax.figure.set_size_inches(20, 10)

    ax.set_xlabel(xlabel=xlabel, fontsize=20)
    ax.set_ylabel(ylabel=ylabel, fontsize=20)
    ax.set_xticklabels(['0-10',
                        '10-20',
                        '20-30',
                        '30-40',
                        '40-50',
                        '50-60',
                        '60-70',
                        '70-80',
                        '80-90',
                        '90-100'],
                       rotation='horizontal',
                       fontsize=20)
    ax.set_yticklabels(
        rotation='horizontal', fontsize=20)
    # ax.set_yticklabels(['20', '40', '60', '70', '80', '100', '120', '140','160'],
    #                    rotation='horizontal',fontsize=30)
    ax.set_title(title, fontsize=20)
    ax.legend(loc='upper left', fontsize=20)
    plt.savefig(outfile)
//  [python]
def plot_x_error(df, x, y, xlabel, ylabel, title, outfile):

    sns.set(style="whitegrid")

    ax = sns.catplot(x=x, y=y, data=df, kind='bar')
    ax.fig.set_size_inches(20, 10)

    axes = ax.axes.flatten()
    axes[0].set_xlabel(xlabel=xlabel, fontsize=20)
    axes[0].set_ylabel(ylabel=ylabel, fontsize=20)
    ax.set_xticklabels(['0-10',
                        '10-20',
                        '20-30',
                        '30-40',
                        '40-50',
                        '50-60',
                        '60-70',
                        '70-80',
                        '80-90',
                        '90-100'],
                       rotation='horizontal',
                       fontsize=20)
    ax.set_yticklabels(
        rotation='horizontal', fontsize=20)
    # ax.set_yticklabels(['20', '40', '60', '70', '80', '100', '120', '140','160'],
    #                    rotation='horizontal',fontsize=30)
    axes[0].set_title(title, fontsize=20)
    axes[0].legend(loc='upper left', fontsize=20)
    plt.savefig(outfile)
//  [python]
def plot_occ_error(df, x, y, xlabel, ylabel, title, outfile):

    sns.set(style="whitegrid")

    ax = sns.barplot(x=x, y=y, data=df)
    ax.figure.set_size_inches(20, 10)

    ax.set_xlabel(xlabel=xlabel, fontsize=20)
    ax.set_ylabel(ylabel=ylabel, fontsize=20)

    ax.set_xticklabels(['0-10',
                        '10-20',
                        '20-30',
                        '30-40',
                        '40-50',
                        '50-60',
                        '60-70',
                        '70-80',
                        '80-90',
                        '90-100'],
                       rotation='horizontal',
                       fontsize=20)
    ax.set_yticklabels(
        rotation='horizontal', fontsize=20)
    ax.set_title(title, fontsize=20)
    ax.legend(loc='upper left', fontsize=20)
    plt.savefig(outfile)
//  [python]
def plot_occ_error(df, x, y, xlabel, ylabel, title, outfile):

    sns.set(style="whitegrid")

    ax = sns.catplot(x=x, y=y, data=df, kind='bar')
    ax.fig.set_size_inches(20, 10)

    axes = ax.axes.flatten()
    axes[0].set_xlabel(xlabel=xlabel, fontsize=20)
    axes[0].set_ylabel(ylabel=ylabel, fontsize=20)

    ax.set_xticklabels(['0-10',
                        '10-20',
                        '20-30',
                        '30-40',
                        '40-50',
                        '50-60',
                        '60-70',
                        '70-80',
                        '80-90',
                        '90-100'],
                       rotation='horizontal',
                       fontsize=20)
    ax.set_yticklabels(
        rotation='horizontal', fontsize=20)
    axes[0].set_title(title, fontsize=20)
    axes[0].legend(loc='upper left', fontsize=20)
    plt.savefig(outfile)
//  [python]
def cat96_relative_plot(df, divide_category=False, cat_name=None, y=1.):
    '''plot the 96 categories, without information from gnomAD or SSC; most
    likely this will be integrated in different plot that this one similar to
    what was done for the NMed paper.'''
    
    #generates table with all the categories, so nothing is left out
    df = make_table_TRIs_CATs(df, divide_category, cat_name)
    #removes all the CAT counts   
    df = df[df.TRI_CAT != '6_CAT']
    
    #sum all categories or only one depending on input
    if divide_category == True:
        df['SUM'] = df.groupby(cat_name)['POS'].transform('sum')
    else:
        df['SUM'] = df['POS'].sum()

    df['REL_COUNT'] = df.POS/df.SUM
    
    #make several plots or only one depending on categories
    if divide_category == True:
        f, axs = plt.subplots(nrows=len(df[cat_name].unique()), ncols=6,
                              sharex=True)
    else:
        f, axs = plt.subplots(nrows=2, ncols=6, sharex=True)
    
    
    poss_vars = list(set(df['CAT'].values.tolist()))
    poss_vars.sort()
    
    colors = ['xkcd:light blue', 'xkcd:black', 'xkcd:bright red',
              'xkcd:light gray', 'xkcd:slime green', 'xkcd:soft pink']
    
    for i, cat in enumerate(df[cat_name].sort_values().unique()):

        for j, var in enumerate(poss_vars):
            
            sns.barplot(x='TRI_CAT', y='REL_COUNT',
                            data=df[(df.CAT == var) & (df[cat_name] == cat)],
                            color=colors[j], ax=axs[i,j])
            
            
            if j == 0:
                axs[i,0].set_ylim(0, y)
                sns.despine(offset=5, trim=True, ax=axs[i,0])
                axs[i,0].set_ylabel('Relative Contribution')
                axs[i,j].set_xlabel(var)
            else:
                sns.despine(offset=5, trim=True, ax=axs[i,j], left=True)
                axs[i,j].set_ylim(0, y)
                axs[i,j].set_yticks([])
                axs[i,j].set_ylabel('')
                    
                axs[i,j].set_xlabel(var)
                axs[i,j].set_xticks([])
                axs[i,j].set_title('')
        
        plt.show()
//  [python]
def cat96_relative_plot(df, divide_category=False, cat_name=None, y=1.):
    '''plot the 96 categories, without information from gnomAD or SSC; most
    likely this will be integrated in different plot that this one similar to
    what was done for the NMed paper.'''
    
    #generates table with all the categories, so nothing is left out
    df = make_table_TRIs_CATs(df, divide_category, cat_name)
    #removes all the CAT counts   
    df = df[df.TRI_CAT != '6_CAT']
    
    #sum all categories or only one depending on input
    if divide_category == True:
        df['SUM'] = df.groupby(cat_name)['POS'].transform('sum')
    else:
        df['SUM'] = df['POS'].sum()

    df['REL_COUNT'] = df.POS/df.SUM
    
    #make several plots or only one depending on categories
    if divide_category == True:
        f, axs = plt.subplots(nrows=len(df[cat_name].unique()), ncols=6,
                              sharex=True)
    else:
        f, axs = plt.subplots(nrows=2, ncols=6, sharex=True)
    
    
    poss_vars = list(set(df['CAT'].values.tolist()))
    poss_vars.sort()
    
    colors = ['xkcd:light blue', 'xkcd:black', 'xkcd:bright red',
              'xkcd:light gray', 'xkcd:slime green', 'xkcd:soft pink']
    
    for i, cat in enumerate(df[cat_name].sort_values().unique()):

        for j, var in enumerate(poss_vars):
            
            g = sns.catplot(x='TRI_CAT', y='REL_COUNT',
                            data=df[(df.CAT == var) & (df[cat_name] == cat)],
                            kind='bar', color=colors[j], ax=axs[i,j])
            
            plt.close(g.fig)
            
            if j == 0:
                axs[i,0].set_ylim(0, y)
                sns.despine(offset=5, trim=True, ax=axs[i,0])
                axs[i,0].set_ylabel('Relative Contribution')
                axs[i,j].set_xlabel(var)
            else:
                sns.despine(offset=5, trim=True, ax=axs[i,j], left=True)
                axs[i,j].set_ylim(0, y)
                axs[i,j].set_yticks([])
                axs[i,j].set_ylabel('')
                    
                axs[i,j].set_xlabel(var)
                axs[i,j].set_xticks([])
                axs[i,j].set_title('')
        
        plt.show()
//  [python]
def plot_ori_error(df, x, y, xlabel, ylabel, title, outfile):
    sns.set(style="whitegrid")

    ax = sns.barplot(x=x, y=y, data=df)
    ax.figure.set_size_inches(20, 10)

    ax.set_xlabel(xlabel=xlabel, fontsize=20)
    ax.set_ylabel(ylabel=ylabel, fontsize=20)

    ax.set_xticklabels(['0$\\degree$-45$\\degree$',
                        '45$\\degree$-90$\\degree$',
                        '90$\\degree$-135$\\degree$',
                        '135$\\degree$-180$\\degree$',
                        '180$\\degree$-225$\\degree$',
                        '225$\\degree$-270$\\degree$',
                        '270$\\degree$-315$\\degree$',
                        '315$\\degree$-360$\\degree$'],
                       rotation=45,
                       fontsize=20)

    ax.set_title(title, fontsize=20)
    ax.legend(loc='upper left', fontsize=18)

    plt.savefig(outfile)
//  [python]
def plot_ori_error(df, x, y, xlabel, ylabel, title, outfile):
    sns.set(style="whitegrid")

    ax = sns.catplot(x=x, y=y, data=df, kind='bar')
    ax.fig.set_size_inches(20, 10)

    axes = ax.axes.flatten()
    axes[0].set_xlabel(xlabel=xlabel, fontsize=20)
    axes[0].set_ylabel(ylabel=ylabel, fontsize=20)

    ax.set_xticklabels(['0$\\degree$-45$\\degree$',
                        '45$\\degree$-90$\\degree$',
                        '90$\\degree$-135$\\degree$',
                        '135$\\degree$-180$\\degree$',
                        '180$\\degree$-225$\\degree$',
                        '225$\\degree$-270$\\degree$',
                        '270$\\degree$-315$\\degree$',
                        '315$\\degree$-360$\\degree$'],
                       rotation=45,
                       fontsize=20)

    axes[0].set_title(title, fontsize=20)
    axes[0].legend(loc='upper left', fontsize=18)

    plt.savefig(outfile)
//  [python]
sns.set(style="darkgrid")

titanic = sns.load_dataset("titanic")
print(titanic)

# 4.1 示例
sns.factorplot(x="sex", y="survived", hue="class", kind="bar", data=titanic)
//  [python]
sns.set(style="darkgrid")

titanic = sns.load_dataset("titanic")
print(titanic)

# 4.1 示例
sns.catplot(x="sex", y="survived", hue="class", kind="bar", data=titanic)
//  [python]
def plot_avg(df_avgweight, col_wrap=10, col_order=None,
             height=1.5, scale=0.2, adjust_top=0.96,
             title='Annual Average Weight of Topics Over Time',
             path=None):

    g = sns.FacetGrid(df_avgweight, col='topic', col_wrap=col_wrap, col_order=col_order, height=height, aspect=1)
    g.map(sns.pointplot, "year", "weight", dodge=False, scale=scale, markers='.', s=1)

    g.fig.suptitle(title, fontsize=16)
    g.set_xticklabels(visible=False)
    g.set_yticklabels(visible=False)
    plt.tight_layout(pad=0, w_pad=0, h_pad=0.6)
    g.fig.subplots_adjust(top=adjust_top)

    if path != None:
        plt.savefig(path, dpi=dpi)
        plt.show()
        plt.close()
        return

    plt.show()
//  [python]
def plot_avg(df_avgweight, col_wrap=10, col_order=None,
             height=1.5, scale=0.2, adjust_top=0.96,
             title='Annual Average Weight of Topics Over Time',
             path=None):

    g = sns.catplot(x="year", y='weight',  col='topic',
                    col_wrap=col_wrap, col_order=col_order,
                    kind='point', height=height, aspect=1,
                    dodge=False, s=1, legend=False,
                    markers='.', scale=scale,
                    data=df_avgweight)

    g.fig.suptitle(title, fontsize=16)
    g.set_xticklabels(visible=False)
    g.set_yticklabels(visible=False)
    plt.tight_layout(pad=0, w_pad=0, h_pad=0.6)
    g.fig.subplots_adjust(top=adjust_top)

    if path != None:
        plt.savefig(path, dpi=dpi)
        plt.show()
        plt.close()
        return

    plt.show()
//  [python]
def plot_results(df, target_lib="", audio_format="", ext="png"):
    sns.set_style("whitegrid")

    ordered_libs = df.time.groupby(
        df.lib
    ).mean().sort_values().index.tolist()

    fig = plt.figure()

    g = sns.factorplot(
        x="duration", 
        y="time", 
        kind='point',
        hue_order=ordered_libs,
        hue='lib', 
        data=df,
        size=6.6, 
        aspect=1,
    )

    g.savefig("benchmark_%s_%s_dur.%s" % (target_lib, audio_format, ext))

    fig = plt.figure()
    ax = sns.barplot(x="time", y="lib", data=df, order=ordered_libs, orient='h')
    fig.savefig("benchmark_%s_%s_bar.%s" % (target_lib, audio_format, ext), bbox_inches='tight')
//  [python]
def plot_results(df, target_lib="", audio_format="", ext="png"):
    sns.set_style("whitegrid")

    ordered_libs = df.time.groupby(
        df.lib
    ).mean().sort_values().index.tolist()

    fig = plt.figure()

    g = sns.catplot(
        x="duration", 
        y="time", 
        kind='point',
        hue_order=ordered_libs,
        hue='lib', 
        data=df,
        height=6.6, 
        aspect=1,
    )

    g.savefig("benchmark_%s_%s_dur.%s" % (target_lib, audio_format, ext))

    fig = plt.figure()
    ax = sns.barplot(x="time", y="lib", data=df, order=ordered_libs, orient='h')
    fig.savefig("benchmark_%s_%s_bar.%s" % (target_lib, audio_format, ext), bbox_inches='tight')
//  [python]
def plot_topics(df, kind='', col_order=None, adjust_top=0.97, title=None, height=3.5, col_wrap=2):
    if kind == 'scatter':
        g = sns.FacetGrid(df, col='keywords', col_wrap=col_wrap, col_order=col_order, height=height, aspect=2)
        g.map(sns.stripplot, "year", 'weight', 'topic', jitter=1, dodge=False, s=4, alpha=0.5, edgecolors='w')
        g.fig.suptitle("Dominant Topics Distribution of {}".format(title), fontsize=16)

    elif kind == 'bar':
        g = sns.FacetGrid(df, col='keywords', col_wrap=col_wrap, col_order=col_order, height=height, aspect=2)
        g.map(sns.countplot, "year", hue='topic', dodge=False)
        g.fig.suptitle("Dominant Topics Count of {}".format(title), fontsize=16)

    elif kind == 'point':
        g = sns.FacetGrid(df, col='keywords', col_wrap=col_wrap, col_order=col_order, height=height, aspect=2)
        g.map(sns.pointplot, "year", 'weight', dodge=False, s=1, markers='.', scale=0.5)
        g.fig.suptitle("Average Weight of {}".format(title), fontsize=16)

    elif kind == 'region':
        g = sns.FacetGrid(df, col='region', col_wrap=col_wrap, col_order=col_order, height=height, aspect=2)
        g.map(sns.pointplot, "year", 'weight', dodge=False, s=1, markers='.', scale=0.5)
        g.fig.suptitle("Average Weight of topic {}".format(title), fontsize=16)

    else:
        print('wrong kind.')

    g.set_xticklabels(rotation=90, step=2)
    g.fig.tight_layout(pad=0, w_pad=0, h_pad=0.5)
    g.fig.subplots_adjust(top=adjust_top)
    return g
//  [python]
def plot_topics(df, kind='', col_order=None, adjust_top=0.97, title=None, height=3.5, col_wrap=2):
    if kind == 'scatter':
        g = sns.catplot(x="year", y='weight', hue="topic",
                        col='keywords', col_wrap=col_wrap, col_order=col_order,
                        kind='strip', height=height, aspect=2, jitter=1, dodge=False, legend=False,
                        s=4, alpha=0.5, edgecolors='w',
                        data=df)
        g.fig.suptitle("Dominant Topics Distribution of {}".format(title), fontsize=16)

    elif kind == 'bar':
        g = sns.catplot(x="year", hue="topic",
                        col='keywords', col_wrap=col_wrap, col_order=col_order,
                        kind='count', height=height, aspect=2, dodge=False, legend=False,
                        data=df)
        g.fig.suptitle("Dominant Topics Count of {}".format(title), fontsize=16)

    elif kind == 'point':
        g = sns.catplot(x="year", y='weight',
                        col='keywords', col_wrap=col_wrap, col_order=col_order,
                        kind='point', height=height, aspect=2, dodge=False, s=1, legend=False,
                        markers='.', scale=0.5,
                        data=df)
        g.fig.suptitle("Average Weight of {}".format(title), fontsize=16)

    elif kind == 'region':
        g = sns.catplot(x="year", y='weight',
                        col='region', col_wrap=col_wrap, col_order=col_order,
                        kind='point', height=height, aspect=2, dodge=False, s=1, legend=False,
                        markers='.', scale=0.5,
                        data=df)
        g.fig.suptitle("Average Weight of topic {}".format(title), fontsize=16)

    else:
        print('wrong kind.')

    g.set_xticklabels(rotation=90, step=2)
    g.fig.tight_layout(pad=0, w_pad=0, h_pad=0.5)
    g.fig.subplots_adjust(top=adjust_top)
    return g
//  [python]
def visualize_posdistribution(tag2importance, outfile):
    means = []
    stds = []
    num_instances = []

    function_word_tags = ["ADP", "AUX", "CCONJ", "DET", "NUM", "PART", "PRON", "SCONJ"]
    other_tags = ["PUNCT", "SYM", "X"]
    # Selection and order can be changed for the plot
    #labels = ["ADJ", "ADV", "NOUN", "VERB", "INTJ", "ADP", "AUX", "CONJ", "DET", "PART", "PRON", "NUM"]
    labels = ["ADJ", "ADV", "NOUN", "VERB", "ADP", "AUX", "CONJ", "DET", "PART", "PRON", "NUM"]
    for label in labels:
        if label not in other_tags:
            if label == "CONJ":
                values = tag2importance["CCONJ"] + tag2importance["SCONJ"]
            else:
                values = tag2importance[label]
            #print(label, len(values))
            mean = np.nanmean(values)
            std = np.nanstd(values)

            means.append(mean)
            stds.append(std)
            num_instances.append(len(values))
            #print(f"Mean: {mean:.4f}, {std:.4f}")

    data = pd.DataFrame({"PosTag": labels, "Mean": means, "Std": stds})

    sns.set(font_scale=2)
    fig = sns.factorplot(x="PosTag", y="Mean", data=data, kind="bar", size=7, aspect=2)
    plt.ylim(0.0,0.055)
    plt.xlabel("")
    plt.ylabel("Relative Importance")

    # We added this type of plot (including number of instances per plot) to the appendix
    ax = fig.facet_axis(0, 0)
    for i,p in enumerate(ax.patches):
        ax.text(p.get_x() - 0.01, p.get_height() * 1.02, str(num_instances[i]), color='black',rotation='horizontal',fontsize=16)

    # Save the figure and show
    plt.savefig(outfile, bbox_inches="tight")
    plt.close()
//  [python]
def visualize_posdistribution(tag2importance, outfile):
    means = []
    stds = []
    num_instances = []

    function_word_tags = ["ADP", "AUX", "CCONJ", "DET", "NUM", "PART", "PRON", "SCONJ"]
    other_tags = ["PUNCT", "SYM", "X"]
    # Selection and order can be changed for the plot
    #labels = ["ADJ", "ADV", "NOUN", "VERB", "INTJ", "ADP", "AUX", "CONJ", "DET", "PART", "PRON", "NUM"]
    labels = ["ADJ", "ADV", "NOUN", "VERB", "ADP", "AUX", "CONJ", "DET", "PART", "PRON", "NUM"]
    for label in labels:
        if label not in other_tags:
            if label == "CONJ":
                values = tag2importance["CCONJ"] + tag2importance["SCONJ"]
            else:
                values = tag2importance[label]
            #print(label, len(values))
            mean = np.nanmean(values)
            std = np.nanstd(values)

            means.append(mean)
            stds.append(std)
            num_instances.append(len(values))
            #print(f"Mean: {mean:.4f}, {std:.4f}")

    data = pd.DataFrame({"PosTag": labels, "Mean": means, "Std": stds})

    sns.set(font_scale=2)
    fig = sns.catplot(x="PosTag", y="Mean", data=data, kind="bar", height=7, aspect=2)
    plt.ylim(0.0,0.055)
    plt.xlabel("")
    plt.ylabel("Relative Importance")

    # We added this type of plot (including number of instances per plot) to the appendix
    ax = fig.facet_axis(0, 0)
    for i,p in enumerate(ax.patches):
        ax.text(p.get_x() - 0.01, p.get_height() * 1.02, str(num_instances[i]), color='black',rotation='horizontal',fontsize=16)

    # Save the figure and show
    plt.savefig(outfile, bbox_inches="tight")
    plt.close()
//  [python]
def mappings_frequency(df, filepath=None):
    """
    Plots the frequency of logical conjunction mappings

    Parameters
    ----------
    df: `pandas.DataFrame`_
        DataFrame with columns `frequency` and `mapping`

    filepath: str
        Absolute path to a folder where to write the plot


    Returns
    -------
    plot
        Generated plot


    .. _pandas.DataFrame: http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe
    """

    df = df.sort_values('frequency')
    df['conf'] = df.frequency.map(lambda f: 0 if f < 0.2 else 1 if f < 0.8 else 2)

    g = sns.factorplot(x="mapping", y="frequency", data=df, aspect=3, hue='conf',
            legend=False, kind="point")
    for tick in g.ax.get_xticklabels():
        tick.set_rotation(90)

    g.ax.set_ylim([-.05, 1.05])

    g.ax.set_xlabel("Logical mapping")
    g.ax.set_ylabel("Frequency")

    if filepath:
        g.savefig(os.path.join(filepath, 'mappings-frequency.pdf'))

    return g