def _plot_categories_as_colorblocks(groupby_ax, obs_tidy, colors=None, orientation='left', cmap_name='tab20'):
    """
    Plots categories as colored blocks. If orientation is 'left', the categories are plotted vertically, otherwise
    they are plotted horizontally.

    Parameters
    ----------
    groupby_ax : matplotlib ax
    obs_tidy
    colors : list of valid color names optional (default: `None`)
        Color to use for each category.
    orientation : `str`, optional (default: `left`)
    cmap_name : `str`
        Name of colormap to use, in case colors is None

    Returns
    -------
    ticks position, labels, colormap
    """

    groupby = obs_tidy.index.name
    from matplotlib.colors import ListedColormap, BoundaryNorm
    if colors is None:
        groupby_cmap = pl.get_cmap(cmap_name)
    else:
        groupby_cmap = ListedColormap(colors, groupby + '_cmap')
    norm = BoundaryNorm(np.arange(groupby_cmap.N+1)-.5, groupby_cmap.N)

    # determine groupby label positions such that they appear
    # centered next/below to the color code rectangle assigned to the category
    value_sum = 0
    ticks = []  # list of centered position of the labels
    labels = []
    label2code = {}  # dictionary of numerical values asigned to each label
    for code, (label, value) in enumerate(obs_tidy.index.value_counts(sort=False).iteritems()):
        ticks.append(value_sum + (value / 2))
        labels.append(label)
        value_sum += value
        label2code[label] = code

    groupby_ax.grid(False)

    if orientation == 'left':
        groupby_ax.imshow(np.matrix([label2code[lab] for lab in obs_tidy.index]).T, aspect='auto', cmap=groupby_cmap, norm=norm)
        if len(labels) > 1:
            groupby_ax.set_yticks(ticks)
            groupby_ax.set_yticklabels(labels)

        # remove y ticks
        groupby_ax.tick_params(axis='y', left=False, labelsize='small')
        # remove x ticks and labels
        groupby_ax.tick_params(axis='x', bottom=False, labelbottom=False)

        # remove surrounding lines
        groupby_ax.spines['right'].set_visible(False)
        groupby_ax.spines['top'].set_visible(False)
        groupby_ax.spines['left'].set_visible(False)
        groupby_ax.spines['bottom'].set_visible(False)

        groupby_ax.set_ylabel(groupby)
    else:
        groupby_ax.imshow(np.matrix([label2code[lab] for lab in obs_tidy.index]), aspect='auto', cmap=groupby_cmap, norm=norm)
        if len(labels) > 1:
            groupby_ax.set_xticks(ticks)
            if max([len(x) for x in labels]) < 3:
                # if the labels are small do not rotate them
                rotation = 0
            else:
                rotation = 90
            groupby_ax.set_xticklabels(labels, rotation=rotation)

        # remove x ticks
        groupby_ax.tick_params(axis='x', bottom=False, labelsize='small')
        # remove y ticks and labels
        groupby_ax.tick_params(axis='y', left=False, labelleft=False)

        # remove surrounding lines
        groupby_ax.spines['right'].set_visible(False)
        groupby_ax.spines['top'].set_visible(False)
        groupby_ax.spines['left'].set_visible(False)
        groupby_ax.spines['bottom'].set_visible(False)

        groupby_ax.set_xlabel(groupby)

    return ticks, labels, groupby_cmap, norm
def _plot_categories_as_colorblocks ( groupby_ax , obs_tidy , colors = None , orientation = 'left' , cmap_name = 'tab20' ) : groupby = obs_tidy . index . name from matplotlib . colors import ListedColormap , BoundaryNorm if colors is None : groupby_cmap = pl . get_cmap ( cmap_name ) else : groupby_cmap = ListedColormap ( colors , groupby + '_cmap' ) norm = BoundaryNorm ( np . arange ( groupby_cmap . N + 1 ) - .5 , groupby_cmap . N ) value_sum = 0 ticks = [ ] labels = [ ] label2code = { } for code , ( label , value ) in enumerate ( obs_tidy . index . value_counts ( sort = False ) . iteritems ( ) ) : ticks . append ( value_sum + ( value / 2 ) ) labels . append ( label ) value_sum += value label2code [ label ] = code groupby_ax . grid ( False ) if orientation == 'left' : groupby_ax . imshow ( np . matrix ( [ label2code [ lab ] for lab in obs_tidy . index ] ) . T , aspect = 'auto' , cmap = groupby_cmap , norm = norm ) if len ( labels ) > 1 : groupby_ax . set_yticks ( ticks ) groupby_ax . set_yticklabels ( labels ) groupby_ax . tick_params ( axis = 'y' , left = False , labelsize = 'small' ) groupby_ax . tick_params ( axis = 'x' , bottom = False , labelbottom = False ) groupby_ax . spines [ 'right' ] . set_visible ( False ) groupby_ax . spines [ 'top' ] . set_visible ( False ) groupby_ax . spines [ 'left' ] . set_visible ( False ) groupby_ax . spines [ 'bottom' ] . set_visible ( False ) groupby_ax . set_ylabel ( groupby ) else : groupby_ax . imshow ( np . matrix ( [ label2code [ lab ] for lab in obs_tidy . index ] ) , aspect = 'auto' , cmap = groupby_cmap , norm = norm ) if len ( labels ) > 1 : groupby_ax . set_xticks ( ticks ) if max ( [ len ( x ) for x in labels ] ) < 3 : rotation = 0 else : rotation = 90 groupby_ax . set_xticklabels ( labels , rotation = rotation ) groupby_ax . tick_params ( axis = 'x' , bottom = False , labelsize = 'small' ) groupby_ax . tick_params ( axis = 'y' , left = False , labelleft = False ) groupby_ax . spines [ 'right' ] . set_visible ( False ) groupby_ax . spines [ 'top' ] . set_visible ( False ) groupby_ax . spines [ 'left' ] . set_visible ( False ) groupby_ax . spines [ 'bottom' ] . set_visible ( False ) groupby_ax . set_xlabel ( groupby ) return ticks , labels , groupby_cmap , norm
Plots categories as colored blocks. If orientation is 'left', the categories are plotted vertically, otherwise
    they are plotted horizontally.

    Parameters
    ----------
    groupby_ax : matplotlib ax
    obs_tidy
    colors : list of valid color names optional (default: `None`)
        Color to use for each category.
    orientation : `str`, optional (default: `left`)
    cmap_name : `str`
        Name of colormap to use, in case colors is None

    Returns
    -------
    ticks position, labels, colormap