Generate bar plot of the autocorrelation function for a series (usually an MCMC trace).

    :Arguments:
        data: PyMC object, trace or array
            A trace from an MCMC sample or a PyMC object with one or more traces.

        name: string
            The name of the object.

        maxlags (optional): int
            The largest discrete value for the autocorrelation to be calculated (defaults to 100).

        format (optional): string
            Graphic output format (defaults to png).

        suffix (optional): string
            Filename suffix.

        path (optional): string
            Specifies location for saving plots (defaults to local directory).

        fontmap (optional): dict
            Font mapping for plot labels; most users should not specify this.

        verbose (optional): int
            Level of output verbosity.

def autocorrelation(
    data, name, maxlags=100, format='png', reflected=False, suffix='-acf', path='./',
        fontmap=None, new=True, last=True, rows=1, columns=1, num=1, verbose=1):
    """
    Generate bar plot of the autocorrelation function for a series (usually an MCMC trace).

    :Arguments:
        data: PyMC object, trace or array
            A trace from an MCMC sample or a PyMC object with one or more traces.

        name: string
            The name of the object.

        maxlags (optional): int
            The largest discrete value for the autocorrelation to be calculated (defaults to 100).

        format (optional): string
            Graphic output format (defaults to png).

        suffix (optional): string
            Filename suffix.

        path (optional): string
            Specifies location for saving plots (defaults to local directory).

        fontmap (optional): dict
            Font mapping for plot labels; most users should not specify this.

        verbose (optional): int
            Level of output verbosity.

    """
    # Internal plotting specification for handling nested arrays

    if fontmap is None:
        fontmap = {1: 10, 2: 8, 3: 6, 4: 5, 5: 4}

    # Stand-alone plot or subplot?
    standalone = rows == 1 and columns == 1 and num == 1

    if standalone:
        if verbose > 0:
            print_('Plotting', name)
        figure()

    subplot(rows, columns, num)
    if ndim(data) == 1:
        maxlags = min(len(data) - 1, maxlags)
        try:
            acorr(data, detrend=mlab.detrend_mean, maxlags=maxlags)
        except:
            print_('Cannot plot autocorrelation for %s' % name)
            return

        # Set axis bounds
        ylim(-.1, 1.1)
        xlim(-maxlags * reflected or 0, maxlags)

        # Plot options
        title(
            '\n\n   %s acorr' %
            name,
            x=0.,
            y=1.,
            ha='left',
            va='top',
            fontsize='small')

        # Smaller tick labels
        tlabels = gca().get_xticklabels()
        setp(tlabels, 'fontsize', fontmap[1])

        tlabels = gca().get_yticklabels()
        setp(tlabels, 'fontsize', fontmap[1])
    elif ndim(data) == 2:
        # generate acorr plot for each dimension
        rows = data.shape[1]
        for j in range(rows):
            autocorrelation(
                data[:,
                     j],
                '%s_%d' % (name,
                           j),
                maxlags,
                fontmap=fontmap,
                rows=rows,
                columns=1,
                num=j + 1)
    else:
        raise ValueError(
            'Only 1- and 2- dimensional functions can be displayed')

    if standalone:
        if not os.path.exists(path):
            os.mkdir(path)
        if not path.endswith('/'):
            path += '/'
        # Save to fiel
        savefig("%s%s%s.%s" % (path, name, suffix, format))