JuliaPlots / StatsPlots.jl

Statistical plotting recipes for Plots.jl
Other
437 stars 88 forks source link

MethodError (pdf is ambiguous) when using wand bins (histogram) #479

Closed diegozea closed 2 years ago

diegozea commented 2 years ago

The following is an example of the error:

Screenshot from 2021-12-10 13-42-20

You can reproduce it using:

import Pkg; Pkg.activate(temp=true); Pkg.add(["StatsPlots", "RDatasets"])
using StatsPlots, RDatasets
iris = dataset("datasets", "iris")
@df iris histogram(:PetalLength, bins=:wand)

The error is:

julia> @df iris histogram(:PetalLength, bins=:wand)
ERROR: MethodError: pdf(::Distributions.Normal{Float64}, ::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}) is ambiguous. Candidates:
  pdf(d::Distributions.Distribution{Distributions.ArrayLikeVariate{N}, S} where S<:Distributions.ValueSupport, x::AbstractArray{var"#s11", M} where var"#s11"<:Real) where {N, M} in Distributions at /home/diego/.julia/packages/Distributions/hnQlR/src/common.jl:249
  pdf(d::Distributions.UnivariateDistribution{S} where S<:Distributions.ValueSupport, X::AbstractArray) in Distributions at deprecated.jl:70
Possible fix, define
  pdf(::Distributions.UnivariateDistribution{S} where S<:Distributions.ValueSupport, ::AbstractArray{T, M} where T<:Real) where M
Stacktrace:
  [1] bkfe(gcounts::Vector{Float64}, drv::Int64, bandwidth::Float64, range_x::Vector{Float64})
    @ StatsPlots ~/.julia/packages/StatsPlots/dFNaF/src/hist.jl:215
  [2] wand_bins(x::Vector{Float64}, scalest::Symbol, gridsize::Int64, range_x::Tuple{Float64, Float64}, trun::Bool)
    @ StatsPlots ~/.julia/packages/StatsPlots/dFNaF/src/hist.jl:147
  [3] wand_bins(x::Vector{Float64})
    @ StatsPlots ~/.julia/packages/StatsPlots/dFNaF/src/hist.jl:122
  [4] wand_edges(::Vector{Float64})
    @ StatsPlots ~/.julia/packages/StatsPlots/dFNaF/src/hist.jl:117
  [5] _auto_binning_nbins(vs::Tuple{Vector{Float64}}, dim::Int64; mode::Symbol)
    @ Plots ~/.julia/packages/Plots/FCM0H/src/recipes.jl:798
  [6] _hist_edge
    @ ~/.julia/packages/Plots/FCM0H/src/recipes.jl:806 [inlined]
  [7] JuliaPlots/StatsPlots.jl#222
    @ ~/.julia/packages/Plots/FCM0H/src/recipes.jl:817 [inlined]
  [8] map
    @ ./tuple.jl:213 [inlined]
  [9] _hist_edges
    @ ~/.julia/packages/Plots/FCM0H/src/recipes.jl:817 [inlined]
 [10] _make_hist(vs::Tuple{Vector{Float64}}, binning::Symbol; normed::Bool, weights::Nothing)
    @ Plots ~/.julia/packages/Plots/FCM0H/src/recipes.jl:836
 [11] macro expansion
    @ ~/.julia/packages/Plots/FCM0H/src/recipes.jl:860 [inlined]
 [12] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, #unused#::Type{Val{:barhist}}, x::Any, y::Any, z::Any)
    @ Plots ~/.julia/packages/RecipesBase/qpxEX/src/RecipesBase.jl:289
 [13] _process_seriesrecipe(plt::Any, plotattributes::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/Bxu2O/src/series_recipe.jl:50
 [14] _process_seriesrecipe(plt::Any, plotattributes::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/Bxu2O/src/series_recipe.jl:60
 [15] _process_seriesrecipes!(plt::Any, kw_list::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/Bxu2O/src/series_recipe.jl:27
 [16] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any)
    @ RecipesPipeline ~/.julia/packages/RecipesPipeline/Bxu2O/src/RecipesPipeline.jl:97
 [17] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any)
    @ Plots ~/.julia/packages/Plots/FCM0H/src/plot.jl:208
 [18] #plot#165
    @ ~/.julia/packages/Plots/FCM0H/src/plot.jl:91 [inlined]
 [19] histogram(args::Any; kw::Any)
    @ Plots ~/.julia/packages/RecipesBase/qpxEX/src/RecipesBase.jl:410
 [20] add_label(argnames::Vector{String}, f::Function, args::Vector{Float64}; kwargs::Base.Iterators.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:bins,), Tuple{Symbol}}})
    @ StatsPlots ~/.julia/packages/StatsPlots/dFNaF/src/df.jl:153
 [21] (::var"#1#2")(257::DataFrame)
    @ Main ./none:0
 [22] top-level scope
    @ REPL[5]:1

The version info is:

(jl_Ow3p6i) pkg> st
      Status `/tmp/jl_Ow3p6i/Project.toml`
  [ce6b1742] RDatasets v0.7.6
  [f3b207a7] StatsPlots v0.14.29

julia> versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
sethaxen commented 2 years ago

histogram is defined in Plots.jl, not StatsPlots.jl.

mkborregaard commented 2 years ago

I wonder if this is really an issue in Distributions? 🤔

diegozea commented 2 years ago

histogram is defined in Plots; however, wand bins are only available when using StatsPlots. But, it could be an issue in Distributions according to the error message.

mkborregaard commented 2 years ago

sorry @sethaxen it actually does belong here, as this is where wand bins are defined. I'll fix.

sethaxen commented 2 years ago

Whoops, sorry for jumping the gun, I'll look into it.

mkborregaard commented 2 years ago

I'm already on it :-) I wrote the code, and it looks like plain bitrot, so I'll also add a test.