Skip to content

Commit

Permalink
Histogram: fix a bug when data changes to allequal values
Browse files Browse the repository at this point in the history
When plotting Observables if the contained collection changed such that
all values were equal the minimum and maximum values would match causing
pick_hist_edges to return a vector. This in turn would result in a
conversion error if the previous value was a range. This is fixed by
simply returning a range instead.

Fixes #2452
  • Loading branch information
Sagnac committed Oct 30, 2023
1 parent 9a28fd0 commit c7db315
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/stats/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function pick_hist_edges(vals, bins)
if bins isa Int
mi, ma = float.(extrema(vals))
if mi == ma
return [mi - 0.5, ma + 0.5]
return range(mi - 0.5, ma + 0.5)
end
# hist is right-open, so to include the upper data point, make the last bin a tiny bit bigger
ma = nextfloat(ma)
Expand Down
16 changes: 16 additions & 0 deletions test/hist.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@testset "Histogram plotting" begin
unequal_vec = [1; rand(2:9, rand(1:9))]
allequal_vec = fill(rand(1:9), rand(1:9))
# normal range
@test_nowarn hist(0:rand(1:9))
# initialize with unequal observable vector
v = Observable(unequal_vec)
@test_nowarn hist(v)
# change to allequal vector
@test_nowarn v[] = allequal_vec
# initialize with allequal observable vector
v = Observable(allequal_vec)
@test_nowarn hist(v)
# change to unequal vector
@test_nowarn v[] = unequal_vec
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ using Makie: volume
include("PolarAxis.jl")
include("barplot.jl")
include("bezier.jl")
include("hist.jl")
end

0 comments on commit c7db315

Please sign in to comment.