Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: add weights keyword to density; close #201 #232

Merged
merged 1 commit into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# density

@recipe function f(::Type{Val{:density}}, x, y, z; trim=false)
newx, newy = violin_coords(y, trim=trim)
newx, newy = violin_coords(y, trim=trim, wts = plotattributes[:weights])
if Plots.isvertical(plotattributes)
newx, newy = newy, newx
end
Expand All @@ -20,7 +20,7 @@ Plots.@deps density path

@recipe function f(::Type{Val{:cdensity}}, x, y, z; trim=false,
npoints = 200)
newx, newy = violin_coords(y, trim=trim)
newx, newy = violin_coords(y, trim=trim, wts = plotattributes[:weights])

if Plots.isvertical(plotattributes)
newx, newy = newy, newx
Expand Down
7 changes: 4 additions & 3 deletions src/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

const _violin_warned = [false]

function violin_coords(y; trim::Bool=false)
kd = KernelDensity.kde(y, npoints = 200)
function violin_coords(y; wts = nothing, trim::Bool=false)

kd = isnothing(wts) ? KernelDensity.kde(y, npoints = 200) : KernelDensity.kde(y, weights = weights(wts), npoints = 200)
if trim
xmin, xmax = Plots.ignorenan_extrema(y)
inside = Bool[ xmin <= x <= xmax for x in kd.x]
Expand Down Expand Up @@ -37,7 +38,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)
widths, centers = violin_coords(fy, trim=trim, wts = plotattributes[:weights])
isempty(widths) && continue

# normalize
Expand Down