Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
conservative API
Browse files Browse the repository at this point in the history
  • Loading branch information
piever committed May 15, 2021
1 parent 7bec63a commit d4ce282
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
7 changes: 5 additions & 2 deletions docs/src/plotting_functions/violin.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ N = 1000
xs = rand(1:3, N)
dodge = rand(1:2, N)
side = rand([:left, :right], N)
color = Observable((left = :orange, right = :teal))
color = @. ifelse(side == :left, :orange, :teal)
ys = map(side) do s
return s == :left ? randn() : rand()
end
Expand All @@ -57,7 +57,10 @@ AbstractPlotting.inline!(true) # hide
N = 1000
xs = rand(1:3, N)
side = rand([:left, :right], N)
color = Observable((left = [:red, :orange, :yellow], right = [:blue, :teal, :cyan]))
color = map(xs, side) do x, s
colors = s == :left ? [:red, :orange, :yellow] : [:blue, :teal, :cyan]
return colors[x]
end
ys = map(side) do s
return s == :left ? randn() : rand()
end
Expand Down
41 changes: 20 additions & 21 deletions src/stats/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ end

conversion_trait(x::Type{<:Violin}) = SampleBased()

function getviolincolor(c, i::Int)
c isa NamedTuple || return c
return i == 1 ? c.right : c.left
getuniquevalue(v, idxs) = v

function getuniquevalue(v::AbstractVector, idxs)
u = view(v, idxs)
f = first(u)
msg = "Collection must have the same value across all indices"
all(isequal(f), u) || throw(ArgumentError(msg))
return f
end

function plot!(plot::Violin)
Expand All @@ -63,7 +68,8 @@ function plot!(plot::Violin)
l1, l2 = limits isa Function ? limits(v) : limits
i1, i2 = searchsortedfirst(k.x, l1), searchsortedlast(k.x, l2)
kde = (x = view(k.x, i1:i2), density = view(k.density, i1:i2))
return (x = key.x, side = key.side, kde = kde, median = median(v))
c = getuniquevalue(color, idxs)
return (x = key.x, side = key.side, color = to_color(c), kde = kde, median = median(v))
end

max = if max_density === automatic
Expand All @@ -77,7 +83,7 @@ function plot!(plot::Violin)

vertices = Vector{Point2f0}[]
lines = Pair{Point2f0, Point2f0}[]
side = Int[]
colors = RGBA{Float32}[]

for spec in specs
scale = 0.5*violinwidth/max
Expand Down Expand Up @@ -108,26 +114,19 @@ function plot!(plot::Violin)
push!(lines, median_left => median_right)
end

push!(side, spec.side)
push!(colors, spec.color)
end

return (vertices = vertices, lines = lines, side = side)
return (vertices = vertices, lines = lines, colors = colors)
end

for i in -1:1
sidevertices = lift(s -> s.vertices[s.side .== i], signals)
sidecolor = lift(c -> getviolincolor(c, i), color)
# TODO: fix empty `poly` to avoid this check
if !isempty(sidevertices[])
poly!(
plot,
sidevertices,
color = sidecolor,
strokecolor = plot[:strokecolor],
strokewidth = plot[:strokewidth],
)
end
end
poly!(
plot,
lift(s -> s.vertices, signals),
color = lift(s -> s.colors, signals),
strokecolor = plot[:strokecolor],
strokewidth = plot[:strokewidth],
)
linesegments!(
plot,
lift(s -> s.lines, signals),
Expand Down

0 comments on commit d4ce282

Please sign in to comment.