import matplotlib.pyplot as _plt
import numpy as _np

__all__ = ["errorhist"]

def errorhist(data,
              bins=10,
              bin_range=None,
              yerr=None,
              normed=True,
              weights=None,
              color=None,
              hist_args={"histtype":"step"},
              err_args={},
              ax=None,
              ):
    """
    Plot a histogram with errorbars
    """

    data = _np.array(data)

    try:
        bins = int(bins)
    except:
        bin_range = [min(bins), max(bins)]
        bins = len(bins) - 1

    if bin_range is None and type(bins) is int:
        bin_range = [_np.min(data), _np.max(data)]

    if ax is None:
        ax = _plt.gca()

    if color is None:
        color = next(ax._get_lines.color_cycle)

    if normed is True:
        mask = _np.logical_and(data>=bin_range[0], data<=bin_range[1])
        num_events = _np.sum(mask)
        normalisation = bins/(num_events*(bin_range[1]-bin_range[0]))
    else:
        normalisation = 1

    histo, bin_edges = _np.histogram(data, bins, bin_range)

    bin_middles = 0.5*(bin_edges[1:] + bin_edges[:-1])
    if yerr is None:
        if weights is None:
            yerr = _np.sqrt(histo)
        else:
            yerr = _np.zeros(bins)
            for i, (a, b) in enumerate(zip(bin_edges[:-1], bin_edges[1:])):
                mask = _np.logical_and(data >= a, data < b)
                yerr[i] = _np.sqrt(_np.sum(weights[mask]**2))

    yerr *= normalisation

    ax.errorbar(
        bin_middles,
        histo*normalisation,
        fmt='none',
        ecolor=color,
        yerr=yerr,
        **err_args
    )
    hist, bin_edges, patches = ax.hist(
        data,
        bins,
        bin_range,
        color=color,
        normed=normed,
        **hist_args
    )

    _plt.draw_if_interactive()
    return hist, bin_edges, patches

@@ -21,17 +21,16 @@
 plot as 
-_
 plt%0Aimpo
@@ -45,12 +45,43 @@
  as 
-_
 np%0A
+from matplotlib.axes import Axes
 %0A__a
@@ -436,16 +436,1134 @@
 rrorbars
+%0A%0A    Arguments%0A    ---------%0A%0A    data : array-like%0A        the data to be filled into the histogram%0A    bins : int or array-like%0A        number of bins if int, bin edges if array-like%0A    bin_range : array-like with shape (2,)%0A        upper and lower limit of the histgram. If None,%0A        min(data) and max(data) are used%0A    yerr : array-like%0A        the errors for the bins, if None sqrt(bin_entries) in%0A        the case without weights is used, for the case with weights%0A        sqrt(sum(weights**2)) is used%0A    weights : array-like%0A        weights for each event in data%0A    normed : boolean%0A        if True, the area under the histogram is normed to 1%0A    color : matplotlib conform color%0A        the color for histogram and errorbars, if None the next color%0A        in the colorcycle is used%0A    hist_args : dict%0A        options for the hist, look into plt.hist documentation%0A    err_args : dict%0A        options for the errorbars, look into plt.errorbar documentation%0A    ax : matplotlib.axes.Axes instance%0A        the axes in which the histogram should be plotted, the current active%0A        figure is used
 %0A    %22%22%22
@@ -2979,16 +2979,16 @@
 ctive()%0A
-
     retu
@@ -3015,8 +3015,336 @@
 patches%0A
+%0Adef hist_from_entries(self, entries, edges, color=None):%0A    if color is None:%0A        color = next(self._get_lines.color_cycle)%0A%0A    xs = np.repeat(edges, 2)%0A    ys = np.concatenate(%5B%5B0%5D, np.repeat(entries, 2), %5B0%5D%5D)%0A    self.fill(xs, ys, closed=False, fill=False, edgecolor=color)%0A%0AAxes.hist_from_entries = hist_from_entries%0A

add docstring