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

Commit d4ce282

Browse files
committed
conservative API
1 parent 7bec63a commit d4ce282

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

docs/src/plotting_functions/violin.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ N = 1000
4141
xs = rand(1:3, N)
4242
dodge = rand(1:2, N)
4343
side = rand([:left, :right], N)
44-
color = Observable((left = :orange, right = :teal))
44+
color = @. ifelse(side == :left, :orange, :teal)
4545
ys = map(side) do s
4646
return s == :left ? randn() : rand()
4747
end
@@ -57,7 +57,10 @@ AbstractPlotting.inline!(true) # hide
5757
N = 1000
5858
xs = rand(1:3, N)
5959
side = rand([:left, :right], N)
60-
color = Observable((left = [:red, :orange, :yellow], right = [:blue, :teal, :cyan]))
60+
color = map(xs, side) do x, s
61+
colors = s == :left ? [:red, :orange, :yellow] : [:blue, :teal, :cyan]
62+
return colors[x]
63+
end
6164
ys = map(side) do s
6265
return s == :left ? randn() : rand()
6366
end

src/stats/violin.jl

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ end
3434

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

37-
function getviolincolor(c, i::Int)
38-
c isa NamedTuple || return c
39-
return i == 1 ? c.right : c.left
37+
getuniquevalue(v, idxs) = v
38+
39+
function getuniquevalue(v::AbstractVector, idxs)
40+
u = view(v, idxs)
41+
f = first(u)
42+
msg = "Collection must have the same value across all indices"
43+
all(isequal(f), u) || throw(ArgumentError(msg))
44+
return f
4045
end
4146

4247
function plot!(plot::Violin)
@@ -63,7 +68,8 @@ function plot!(plot::Violin)
6368
l1, l2 = limits isa Function ? limits(v) : limits
6469
i1, i2 = searchsortedfirst(k.x, l1), searchsortedlast(k.x, l2)
6570
kde = (x = view(k.x, i1:i2), density = view(k.density, i1:i2))
66-
return (x = key.x, side = key.side, kde = kde, median = median(v))
71+
c = getuniquevalue(color, idxs)
72+
return (x = key.x, side = key.side, color = to_color(c), kde = kde, median = median(v))
6773
end
6874

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

7884
vertices = Vector{Point2f0}[]
7985
lines = Pair{Point2f0, Point2f0}[]
80-
side = Int[]
86+
colors = RGBA{Float32}[]
8187

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

111-
push!(side, spec.side)
117+
push!(colors, spec.color)
112118
end
113119

114-
return (vertices = vertices, lines = lines, side = side)
120+
return (vertices = vertices, lines = lines, colors = colors)
115121
end
116122

117-
for i in -1:1
118-
sidevertices = lift(s -> s.vertices[s.side .== i], signals)
119-
sidecolor = lift(c -> getviolincolor(c, i), color)
120-
# TODO: fix empty `poly` to avoid this check
121-
if !isempty(sidevertices[])
122-
poly!(
123-
plot,
124-
sidevertices,
125-
color = sidecolor,
126-
strokecolor = plot[:strokecolor],
127-
strokewidth = plot[:strokewidth],
128-
)
129-
end
130-
end
123+
poly!(
124+
plot,
125+
lift(s -> s.vertices, signals),
126+
color = lift(s -> s.colors, signals),
127+
strokecolor = plot[:strokecolor],
128+
strokewidth = plot[:strokewidth],
129+
)
131130
linesegments!(
132131
plot,
133132
lift(s -> s.lines, signals),

0 commit comments

Comments
 (0)