Fix another hist BoundsError
This fixes the error with `hist([1,1,1], bs=1)`:
ERROR: BoundsError: attempt to access 0-element Vector{Int64} at index [0]
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:1864
[3] top-level scope
@ REPL[50]:1
This commit is contained in:
parent
db8dcfc433
commit
67c8781f2b
@ -1846,15 +1846,19 @@ function hist(v::Vector{T}; range=[NaN,NaN], bs=NaN, nbins=0, pad=true) where T
|
|||||||
if sum(hh.weights) < length(i)
|
if sum(hh.weights) < length(i)
|
||||||
j = findall(v[i] .== range[2])
|
j = findall(v[i] .== range[2])
|
||||||
@assert length(j) == (length(i) - sum(hh.weights))
|
@assert length(j) == (length(i) - sum(hh.weights))
|
||||||
hh.weights[end] += length(j)
|
if length(hh.weights) > 0
|
||||||
|
hh.weights[end] += length(j)
|
||||||
|
else
|
||||||
|
push!(hh.weights, length(j))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
hh = fit(Histogram, v[i], closed=:left)
|
hh = fit(Histogram, v[i], closed=:left)
|
||||||
end
|
end
|
||||||
@assert sum(hh.weights) == length(i)
|
@assert sum(hh.weights) == length(i)
|
||||||
x = collect(hh.edges[1])
|
x = collect(hh.edges[1])
|
||||||
binsize = x[2] - x[1]
|
binsize = isfinite(bs) ? bs : x[2] - x[1]
|
||||||
x = (x[1:end-1] .+ x[2:end]) ./ 2
|
length(x) > 1 && (x = (x[1:end-1] .+ x[2:end]) ./ 2)
|
||||||
h = hh.weights
|
h = hh.weights
|
||||||
if pad
|
if pad
|
||||||
x = [x[1]-binsize, x..., x[end]+binsize]
|
x = [x[1]-binsize, x..., x[end]+binsize]
|
||||||
|
|||||||
@ -288,5 +288,6 @@ Gnuplot.options.dry = true
|
|||||||
|
|
||||||
# Various hist() corner cases
|
# Various hist() corner cases
|
||||||
@gp hist([1,2,3], bs=2)
|
@gp hist([1,2,3], bs=2)
|
||||||
|
@gp hist([1,1,1], bs=1)
|
||||||
|
|
||||||
Gnuplot.quitall()
|
Gnuplot.quitall()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user