Skip to content

Commit

Permalink
Merge pull request #361 from eddieschoute/master
Browse files Browse the repository at this point in the history
Add bandwidth parameter to violin plot
  • Loading branch information
mkborregaard authored Sep 7, 2020
2 parents fa49981 + fbbe891 commit 0b25b88
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

const _violin_warned = [false]

function violin_coords(y; wts = nothing, trim::Bool=false)

kd = wts === nothing ? KernelDensity.kde(y, npoints = 200) : KernelDensity.kde(y, weights = weights(wts), npoints = 200)
function violin_coords(y; wts = nothing, trim::Bool=false, bandwidth = KernelDensity.default_bandwidth(y))
kd = wts === nothing ?
KernelDensity.kde(y, npoints = 200, bandwidth = bandwidth) :
KernelDensity.kde(y, weights = weights(wts), npoints = 200, bandwidth = bandwidth)
if trim
xmin, xmax = Plots.ignorenan_extrema(y)
inside = Bool[ xmin <= x <= xmax for x in kd.x]
Expand All @@ -20,7 +21,7 @@ get_quantiles(x::Real) = [x]
get_quantiles(b::Bool) = b ? [0.5] : Float64[]
get_quantiles(n::Int) = range(0, 1, length = n + 2)[2:end-1]

@recipe function f(::Type{Val{:violin}}, x, y, z; trim=true, side=:both, show_mean = false, show_median = false, quantiles = Float64[])
@recipe function f(::Type{Val{:violin}}, x, y, z; trim=true, side=:both, show_mean = false, show_median = false, quantiles = Float64[], bandwidth = KernelDensity.default_bandwidth(y))
# if only y is provided, then x will be UnitRange 1:size(y,2)
if typeof(x) <: AbstractRange
if step(x) == first(x) == 1
Expand All @@ -38,7 +39,7 @@ get_quantiles(n::Int) = range(0, 1, length = n + 2)[2:end-1]
msc = plotattributes[:markerstrokecolor]
for (i,glabel) in enumerate(glabels)
fy = y[filter(i -> _cycle(x,i) == glabel, 1:length(y))]
widths, centers = violin_coords(fy, trim=trim, wts = plotattributes[:weights])
widths, centers = violin_coords(fy, trim=trim, wts = plotattributes[:weights], bandwidth = bandwidth)
isempty(widths) && continue

# normalize
Expand Down

0 comments on commit 0b25b88

Please sign in to comment.