Statistic errors for histograms with Plots recipes

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
    However, they do not appear
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:

@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.

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:

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:

thanks for the reply.

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.

Doesn’t StatsPlots work for you?

sure, why?

I could not figure out, looks like a bug somewhere