Fix bounds error in hist recipe

When running hist([1,2,3], bs=2), the following error is produced:

ERROR: BoundsError: attempt to access 1-element Vector{Float64} at index [2]
Stacktrace:
 [1] getindex
   @ ./array.jl:801 [inlined]
 [2] hist(v::Vector{Int64}; range::Vector{Float64}, bs::Int64, nbins::Int64, pad::Bool)
   @ Gnuplot ~/.julia/dev/Gnuplot/src/Gnuplot.jl:1873
 [3] top-level scope
   @ REPL[25]:1
This commit is contained in:
Michal Sojka 2021-09-06 15:57:50 +02:00
parent 04484adc22
commit db8dcfc433
2 changed files with 4 additions and 1 deletions

View File

@ -1853,9 +1853,9 @@ function hist(v::Vector{T}; range=[NaN,NaN], bs=NaN, nbins=0, pad=true) where T
end
@assert sum(hh.weights) == length(i)
x = collect(hh.edges[1])
binsize = x[2] - x[1]
x = (x[1:end-1] .+ x[2:end]) ./ 2
h = hh.weights
binsize = x[2] - x[1]
if pad
x = [x[1]-binsize, x..., x[end]+binsize]
h = [0, h..., 0]

View File

@ -286,4 +286,7 @@ Gnuplot.quit(:default)
Gnuplot.options.dry = true
@gp hist(randn(1000))
# Various hist() corner cases
@gp hist([1,2,3], bs=2)
Gnuplot.quitall()