Generates histogram from an array of data.

    :Arguments:
        data: array or list
            Usually a trace from an MCMC sample.

        name: string
            The name of the histogram.

        bins: int or string
            The number of bins, or a preferred binning method. Available methods include
            'doanes', 'sturges' and 'sqrt' (defaults to 'doanes').

        datarange: tuple or list
            Preferred range of histogram (defaults to (None,None)).

        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 map for plot.

def histogram(
    data, name, bins='sturges', datarange=(None, None), format='png', suffix='', path='./', rows=1,
        columns=1, num=1, last=True, fontmap = None, verbose=1):
    """
    Generates histogram from an array of data.

    :Arguments:
        data: array or list
            Usually a trace from an MCMC sample.

        name: string
            The name of the histogram.

        bins: int or string
            The number of bins, or a preferred binning method. Available methods include
            'doanes', 'sturges' and 'sqrt' (defaults to 'doanes').

        datarange: tuple or list
            Preferred range of histogram (defaults to (None,None)).

        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 map for plot.
    """

    # Internal histogram specification for handling nested arrays
    try:

        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_('Generating histogram of', name)
            figure()

        subplot(rows, columns, num)

        # Specify number of bins
        uniquevals = len(unique(data))
        if isinstance(bins, int):
            pass
        if bins == 'sturges':
            bins = uniquevals * (uniquevals <= 25) or _sturges(len(data))
        elif bins == 'doanes':
            bins = uniquevals * (uniquevals <= 25) or _doanes(data, len(data))
        elif bins == 'sqrt':
            bins = uniquevals * (uniquevals <= 25) or _sqrt_choice(len(data))
        elif isinstance(bins, int):
            bins = bins
        else:
            raise ValueError('Invalid bins argument in histogram')

        if isnan(bins):
            bins = uniquevals * (uniquevals <= 25) or int(
                4 + 1.5 * log(len(data)))
            print_(
                'Bins could not be calculated using selected method. Setting bins to %i.' %
                bins)

        # Generate histogram
        hist(data.tolist(), bins, histtype='stepfilled')

        xlim(datarange)

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

        ylabel("Frequency", fontsize='x-small')

        # Plot vertical lines for median and 95% HPD interval
        quant = calc_quantiles(data)
        axvline(x=quant[50], linewidth=2, color='black')
        for q in calc_hpd(data, 0.05):
            axvline(x=q, linewidth=2, color='grey', linestyle='dotted')

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

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

    except OverflowError:
        print_('... cannot generate histogram')