# Vertical errors for historgrams

**URL:** <https://discourse.julialang.org/t/vertical-errors-for-historgrams/45881>\
**Category:** Statistics\
**Tags:** question, plotting, plots, hep\
**Created:** [September 1, 2020, 9:25am UTC](https://discourse.julialang.org/t/vertical-errors-for-historgrams/45881 "2020-09-01T09:25:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [September 1, 2020, 9:25am UTC](https://discourse.julialang.org/t/vertical-errors-for-historgrams/45881/1 "2020-09-01T09:25:46Z")

</div>

In my field of research (HEP), the vertical errors for the histograms are widely used.  
Usually, they are calculated assuming narrow bins and Poisson distribution for the number of entries,  
i.e. the error is `sqrt(Nentries)`

It is clear how to extend `Plots.scatterbins` for such errors.  
While, it gets more complicated (implementation-wise) when the

- data are weighted
- histogram is normalized

Plots use `StatsBase.Histogram` that does not give access to the entries weights inside of individual bins. This information seems to be lost once `Histogram` is created.

Any ideas on the problem?

Here is my `errorhist` recipe for the Poisson error.

```julia
@recipe function f(::Type{Val{:errorhist}}, x, y, z)
    h = Plots._make_hist(
        (y,),
        plotattributes[:bins],
        normed = plotattributes[:normalize],
        weights = plotattributes[:weights],
    )
    x := h.edges[1]
    y := h.weights
    seriestype := :scatterbins
    # 
    @series begin
        primary := false
        seriestype := :yerror
        x := Plots._bin_centers(h.edges[1])
        yerror --> map(x->x < 0 ? 0.0 : sqrt(x), h.weights)
    end
    ()
end
@shorthands errorhist

```

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [September 1, 2020, 9:26am UTC](https://discourse.julialang.org/t/vertical-errors-for-historgrams/45881/2 "2020-09-01T09:26:18Z")

</div>

Related question in `StatsBase`  
[https://github.com/JuliaStats/StatsBase.jl/issues/597](https://github.com/JuliaStats/StatsBase.jl/issues/597)

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [September 1, 2020, 9:27am UTC](https://discourse.julialang.org/t/vertical-errors-for-historgrams/45881/3 "2020-09-01T09:27:06Z")

</div>

Example of `errorhist` in `Plots.jl`  
[https://github.com/JuliaPlots/Plots.jl/issues/2630#issuecomment-623425872](https://github.com/JuliaPlots/Plots.jl/issues/2630#issuecomment-623425872)
