def _handle_ticklabels(ax, k, x_ticklabels, y_ticklabels):
    """
    Handle and format ticks and ticklabels for use in imsubplot.

    The `imsubplot` function creates a figure showing an abitrary number of
    subplots. The `imsubplot` function allows for custom ticklabels along the
    x- and y-axes. This function handles the formatting of the ticklabels for a
    given subplot.

    Parameters
    ----------
    ax : matplotlib.axes.Axes
        The matplotlib axes (subplot) to format ticklabels for.
    k : int
        The axes index, i.e. the subplot number out of the total number of
        subplots.
    x_ticklabels : list or tuple
        The x_ticklabels (as strings or lists of strings) to use for the
        subplots (the default is None, which implies that no x_ticklabels are
        displayed).
    y_ticklabels : list or tuple
        The y_ticklabels (as strings or lists of strings) to use for the
        subplots (the default is None, which implies that no y_ticklabels are
        displayed).

    """
    if x_ticklabels is not None:
        if isinstance(x_ticklabels[0], (list, tuple)):
            x_t = range(len(x_ticklabels[k]))
            x_tl = x_ticklabels[k]
        else:
            x_t = range(len(x_ticklabels))
            x_tl = x_ticklabels
        ax.set_xticks(x_t)
        ax.set_xticklabels(x_tl, rotation=90)
    if y_ticklabels is not None:
        if isinstance(y_ticklabels[0], (list, tuple)):
            y_t = range(len(y_ticklabels[k]))
            y_tl = y_ticklabels[k]
        else:
            y_t = range(len(y_ticklabels))
            y_tl = y_ticklabels
        ax.set_yticks(y_t)
        ax.set_yticklabels(y_tl)
