# Statistic errors for histograms with Plots recipes

**URL:** https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327
**Category:** Visualization
**Tags:** plots, recipe
**Created:** [April 10, 2020, 11:08am UTC](https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327 "2020-04-10T11:08:12Z")
**Posts on this page:** 6
**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: [April 10, 2020, 11:08am UTC](https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327/1 "2020-04-10T11:08:12Z")

</div>

I would love to have a recipe for `Plots.jl` that creates a histogram  
with `xerror` of the size of the bin, and `yerror` according to e.g. `sqrt(h.weight)`.

So, there are two problems:

1. x-errors do not show up. The `scatterhist` is setting `xerror` in `scatterbins`  
[Plots.jl/recipes.jl at master · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/blob/master/src/recipes.jl#L535)  
However, they do not appear

```julia
scatterhist(rand(100)) # does not have x-errors
# but!
h = fit(StatsBase.Histogram, ds)
plot(h, seriestype=:scatterbins) # does have x-errors

```

1. y-errors do not appear, I cannot make the line `yerror := sqrt.(h.weights)` work

here is what I tried:

```julia
@recipe function f(::Type{Val{:errorhist}}, x, y, z)
    h = Plots._make_hist((y,), plotattributes[:bins], normed = plotattributes[:normalize], weights = plotattributes[:weights])
    edge = collect(h.edges)
    # 
    x := Plots._bin_centers(edge)
    y := h.weights
    xerror := diff(edge)/2
    yerror := sqrt.(h.weights)
    seriestype := :scatter
    linewidth := 2
    ()
end
"""
    errorhist
"""
@shorthands errorhist
#
errorhist(rand(1000), bins=100) # empty plot ...

```

EDIT: the plot was empty because `edge = collect(h.edges)` suppose to be `edge = collect(h.edges[1])`. After fixing, the scatter plot appears but without any errorbars.

---

<div class="post-metadata">

### Author: ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)
#### Post date: [April 11, 2020, 11:15am UTC](https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327/2 "2020-04-11T11:15:10Z")

</div>

Does your code work without the recipe macro? If not, and you have a small number of bins, a bar and scatter plot combination might work. Perhaps like the following:

```julia
using Plots
gr(size=(1000,900), xtickfontsize=13, ytickfontsize=13, xguidefontsize=16, yguidefontsize=16, legendfontsize=12, dpi=100, grid=(:y, :gray, :solid, 1, 0.4))
counts = [5,7,8];
barswitherror = bar(["1-3", "4-10", "11 to 19"], counts, legend=false);
scatter!(collect(0.5:1:length(counts)), counts, xerr=[2,6,8] ./ maximum([2,6,8]), yerr=sqrt.(counts), ylim=(0,12), bar_width=5, xlabel="Bins", ylabel="Counts", title="Histogram with x- and y-errors", marker=stroke(1.5))
savefig(barswitherror, "barswitherror.png")

```

And while not perfect, it does produce a histogram-like figure:

 ![barswitherror](https://global.discourse-cdn.com/julialang/original/3X/5/f/5fc7a586eac4cbbdb64b6a41cd956c53ecbcfa11.png)

---

<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: [April 11, 2020, 1:14pm UTC](https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327/3 "2020-04-11T13:14:14Z")

</div>

thanks for the reply.

> [@josephmarturano](#):
>
> Does your code work without the recipe macro?

Sure, I can create a `StatsBase.Histogram` first and, then, plot using the barplot as you suggest. However, It is not the point. One should be able to say `èrrorhist(rand(1000))` and get the right plot.

---

<div class="post-metadata">

### Author: ![phelipe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phelipe/32/14643_2.png) [@phelipe](https://discourse.julialang.org/u/phelipe)
#### Post date: [April 11, 2020, 2:03pm UTC](https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327/4 "2020-04-11T14:03:48Z")

</div>

Doesn’t [StatsPlots](https://github.com/JuliaPlots/StatsPlots.jl) work for you?

---

<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: [April 11, 2020, 6:14pm UTC](https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327/5 "2020-04-11T18:14:33Z")

</div>

sure, why?

---

<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: [April 25, 2020, 4:36pm UTC](https://discourse.julialang.org/t/statistic-errors-for-histograms-with-plots-recipes/37327/6 "2020-04-25T16:36:56Z")

</div>

I could not figure out, looks like a bug somewhere

[https://github.com/JuliaPlots/Plots.jl/issues/2630](https://github.com/JuliaPlots/Plots.jl/issues/2630)
