def rank_genes_groups(adata, groups=None, n_genes=20, gene_symbols=None, key=None, fontsize=8,
                      ncols=4, sharey=True, show=None, save=None, ax=None, **kwds):
    """\
    Plot ranking of genes.

    Parameters
    ----------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    groups : `str` or `list` of `str`
        The groups for which to show the gene ranking.
    gene_symbols : `str`
        Key for field in `.var` that stores gene symbols if you do not want to
        use `.var_names`.
    n_genes : `int`, optional (default: 20)
        Number of genes to show.
    fontsize : `int`, optional (default: 8)
        Fontsize for gene names.
    ncols : `int`, optional (default: 4)
        Number of panels shown per row.
    sharey: `bool`, optional (default: True)
        Controls if the y-axis of each panels should be shared. But passing
        `sharey=False`, each panel has its own y-axis range.
    {show_save_ax}
    """
    if 'n_panels_per_row' in kwds:  n_panels_per_row  = kwds['n_panels_per_row']
    else: n_panels_per_row = ncols
    if key is None: key = 'rank_genes_groups'
    reference = str(adata.uns[key]['params']['reference'])
    group_names = (adata.uns[key]['names'].dtype.names
                   if groups is None else groups)
    # one panel for each group
    # set up the figure
    n_panels_x = min(n_panels_per_row, len(group_names))
    n_panels_y = np.ceil(len(group_names) / n_panels_x).astype(int)

    from matplotlib import gridspec
    fig = pl.figure(figsize=(n_panels_x * rcParams['figure.figsize'][0],
                             n_panels_y * rcParams['figure.figsize'][1]))
    gs = gridspec.GridSpec(nrows=n_panels_y,
                           ncols=n_panels_x,
                           wspace=0.22,
                           hspace=0.3)

    ax0 = None
    ymin = np.Inf
    ymax = -np.Inf
    for count, group_name in enumerate(group_names):
        if sharey is True:
            if ax0 is None:
                ax = fig.add_subplot(gs[count])
                ax0 = ax
            else:
                ax = fig.add_subplot(gs[count], sharey=ax0)
        else:
            ax = fig.add_subplot(gs[count])

        gene_names = adata.uns[key]['names'][group_name]
        scores = adata.uns[key]['scores'][group_name]
        for ig, g in enumerate(gene_names[:n_genes]):
            gene_name = gene_names[ig]
            if adata.raw is not None and adata.uns[key]['params']['use_raw']:
                ax.text(
                    ig, scores[ig],
                    gene_name if gene_symbols is None else adata.raw.var[gene_symbols][gene_name],
                    rotation='vertical', verticalalignment='bottom',
                    horizontalalignment='center', fontsize=fontsize)
            else:
                ax.text(
                    ig, scores[ig],
                    gene_name if gene_symbols is None else adata.var[gene_symbols][gene_name],
                    rotation='vertical', verticalalignment='bottom',
                    horizontalalignment='center', fontsize=fontsize)
        ax.set_title('{} vs. {}'.format(group_name, reference))
        if count >= n_panels_x * (n_panels_y - 1):
            ax.set_xlabel('ranking')

        # print the 'score' label only on the first panel per row.
        if count % n_panels_x == 0:
            ax.set_ylabel('score')

        ax.set_xlim(-0.9, ig + 1-0.1)

        if sharey is True:
            ymin = min(ymin, np.min(scores))
            ymax = max(ymax, np.max(scores))
        else:
            ymin = np.min(scores)
            ymax = np.max(scores)
            ymax += 0.3*(np.max(scores)-np.min(scores))
            ax.set_ylim(ymin, ymax)

    if sharey is True:
        ymax += 0.3*(ymax-ymin)
        ax.set_ylim(ymin, ymax)

    writekey = ('rank_genes_groups_'
                + str(adata.uns[key]['params']['groupby']))
    utils.savefig_or_show(writekey, show=show, save=save)
def rank_genes_groups ( adata , groups = None , n_genes = 20 , gene_symbols = None , key = None , fontsize = 8 , ncols = 4 , sharey = True , show = None , save = None , ax = None , ** kwds ) : if 'n_panels_per_row' in kwds : n_panels_per_row = kwds [ 'n_panels_per_row' ] else : n_panels_per_row = ncols if key is None : key = 'rank_genes_groups' reference = str ( adata . uns [ key ] [ 'params' ] [ 'reference' ] ) group_names = ( adata . uns [ key ] [ 'names' ] . dtype . names if groups is None else groups ) n_panels_x = min ( n_panels_per_row , len ( group_names ) ) n_panels_y = np . ceil ( len ( group_names ) / n_panels_x ) . astype ( int ) from matplotlib import gridspec fig = pl . figure ( figsize = ( n_panels_x * rcParams [ 'figure.figsize' ] [ 0 ] , n_panels_y * rcParams [ 'figure.figsize' ] [ 1 ] ) ) gs = gridspec . GridSpec ( nrows = n_panels_y , ncols = n_panels_x , wspace = 0.22 , hspace = 0.3 ) ax0 = None ymin = np . Inf ymax = - np . Inf for count , group_name in enumerate ( group_names ) : if sharey is True : if ax0 is None : ax = fig . add_subplot ( gs [ count ] ) ax0 = ax else : ax = fig . add_subplot ( gs [ count ] , sharey = ax0 ) else : ax = fig . add_subplot ( gs [ count ] ) gene_names = adata . uns [ key ] [ 'names' ] [ group_name ] scores = adata . uns [ key ] [ 'scores' ] [ group_name ] for ig , g in enumerate ( gene_names [ : n_genes ] ) : gene_name = gene_names [ ig ] if adata . raw is not None and adata . uns [ key ] [ 'params' ] [ 'use_raw' ] : ax . text ( ig , scores [ ig ] , gene_name if gene_symbols is None else adata . raw . var [ gene_symbols ] [ gene_name ] , rotation = 'vertical' , verticalalignment = 'bottom' , horizontalalignment = 'center' , fontsize = fontsize ) else : ax . text ( ig , scores [ ig ] , gene_name if gene_symbols is None else adata . var [ gene_symbols ] [ gene_name ] , rotation = 'vertical' , verticalalignment = 'bottom' , horizontalalignment = 'center' , fontsize = fontsize ) ax . set_title ( '{} vs. {}' . format ( group_name , reference ) ) if count >= n_panels_x * ( n_panels_y - 1 ) : ax . set_xlabel ( 'ranking' ) if count % n_panels_x == 0 : ax . set_ylabel ( 'score' ) ax . set_xlim ( - 0.9 , ig + 1 - 0.1 ) if sharey is True : ymin = min ( ymin , np . min ( scores ) ) ymax = max ( ymax , np . max ( scores ) ) else : ymin = np . min ( scores ) ymax = np . max ( scores ) ymax += 0.3 * ( np . max ( scores ) - np . min ( scores ) ) ax . set_ylim ( ymin , ymax ) if sharey is True : ymax += 0.3 * ( ymax - ymin ) ax . set_ylim ( ymin , ymax ) writekey = ( 'rank_genes_groups_' + str ( adata . uns [ key ] [ 'params' ] [ 'groupby' ] ) ) utils . savefig_or_show ( writekey , show = show , save = save )
\
    Plot ranking of genes.

    Parameters
    ----------
    adata : :class:`~anndata.AnnData`
        Annotated data matrix.
    groups : `str` or `list` of `str`
        The groups for which to show the gene ranking.
    gene_symbols : `str`
        Key for field in `.var` that stores gene symbols if you do not want to
        use `.var_names`.
    n_genes : `int`, optional (default: 20)
        Number of genes to show.
    fontsize : `int`, optional (default: 8)
        Fontsize for gene names.
    ncols : `int`, optional (default: 4)
        Number of panels shown per row.
    sharey: `bool`, optional (default: True)
        Controls if the y-axis of each panels should be shared. But passing
        `sharey=False`, each panel has its own y-axis range.
    {show_save_ax}