From db8dcfc433327405ee1e4e9e9c37cbd935d3c90b Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Mon, 6 Sep 2021 15:57:50 +0200 Subject: [PATCH 1/3] 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 --- src/Gnuplot.jl | 2 +- test/runtests.jl | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Gnuplot.jl b/src/Gnuplot.jl index 18a8057..7c82e50 100644 --- a/src/Gnuplot.jl +++ b/src/Gnuplot.jl @@ -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] diff --git a/test/runtests.jl b/test/runtests.jl index 76a91ed..53fe0fe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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() From 67c8781f2bbdc991fe4b560b1744be9abf32f302 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Mon, 6 Sep 2021 16:41:56 +0200 Subject: [PATCH 2/3] 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 --- src/Gnuplot.jl | 10 +++++++--- test/runtests.jl | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Gnuplot.jl b/src/Gnuplot.jl index 7c82e50..a2d8242 100644 --- a/src/Gnuplot.jl +++ b/src/Gnuplot.jl @@ -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) j = findall(v[i] .== range[2]) @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 else hh = fit(Histogram, v[i], closed=:left) 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 + binsize = isfinite(bs) ? bs : x[2] - x[1] + length(x) > 1 && (x = (x[1:end-1] .+ x[2:end]) ./ 2) h = hh.weights if pad x = [x[1]-binsize, x..., x[end]+binsize] diff --git a/test/runtests.jl b/test/runtests.jl index 53fe0fe..a9a806a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -288,5 +288,6 @@ Gnuplot.options.dry = true # Various hist() corner cases @gp hist([1,2,3], bs=2) +@gp hist([1,1,1], bs=1) Gnuplot.quitall() From ce00617796520790d2d33e919cd7b4c0d4fb5a78 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Mon, 13 Dec 2021 18:58:24 +0100 Subject: [PATCH 3/3] Allow using single quotes in output file names Gnuplot single-quoted strings have to escape single-quote characters by doubling them. --- src/Gnuplot.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gnuplot.jl b/src/Gnuplot.jl index 18a8057..5f83824 100644 --- a/src/Gnuplot.jl +++ b/src/Gnuplot.jl @@ -925,7 +925,7 @@ function execall(gp::GPSession; term::AbstractString="", output::AbstractString= gpexec(gp, "unset multiplot") gpexec(gp, "set term $term") end - (output != "") && gpexec(gp, "set output '$output'") + (output != "") && gpexec(gp, "set output '$(replace(output, "'" => "''"))'") # printstyled("Plotting with terminal: " * terminal() * "\n", color=:blue, bold=true)