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

able to scale violin in different ways #3352

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Unreleased]
- Added `scale` attribute to `violin` [#3352](https://github.com/MakieOrg/Makie.jl/pull/3352).

## [0.20.8] - 2024-02-22

Expand Down
13 changes: 13 additions & 0 deletions ReferenceTests/src/tests/examples2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1392,3 +1392,16 @@ end
ylims!(ax, 0, 1)
fig
end

@reference_test "Violin plots differently scaled" begin
fig = Figure()
xs = vcat([fill(i, i * 1000) for i in 1:4]...)
ys = vcat(randn(6000), randn(4000) * 2)
for (i, scale) in enumerate([:area, :count, :width])
ax = Axis(fig[i, 1])
violin!(ax, xs, ys; scale, show_median=true)
Makie.xlims!(0.2, 4.8)
ax.title = "scale=:$(scale)"
end
fig
end
19 changes: 19 additions & 0 deletions docs/reference/plots/violin.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ violin(categories, values)
```
\end{examplefigure}

\begin{examplefigure}{}
```julia
using Makie, CairoMakie
CairoMakie.activate!() # hide


fig = Figure()
xs = vcat([fill(i, i * 1000) for i in 1:4]...)
ys = vcat(randn(6000), randn(4000) * 2)
for (i, scale) in enumerate([:area, :count, :width])
ax = Axis(fig[i, 1])
violin!(ax, xs, ys; scale, show_median=true)
Makie.xlims!(0.2, 4.8)
ax.title = "scale=:$(scale)"
end
fig
```
\end{examplefigure}

\begin{examplefigure}{}
```julia
using CairoMakie
Expand Down
28 changes: 22 additions & 6 deletions src/stats/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `gap=0.2`: shrinking factor, `width -> width * (1 - gap)`
- `show_median=false`: show median as midline
- `side=:both`: specify `:left` or `:right` to only plot the violin on one side
- `scale=:width`: scale density by area (`:area`), count (`:count`), or width (`:width`).
- `datalimits`: specify values to trim the `violin`. Can be a `Tuple` or a `Function` (e.g. `datalimits=extrema`)
"""
@recipe(Violin, x, y) do scene
Expand All @@ -21,6 +22,7 @@
bandwidth = automatic,
weights = automatic,
side = :both,
scale = :area,
orientation = :vertical,
width = automatic,
dodge = automatic,
Expand Down Expand Up @@ -49,10 +51,10 @@

function plot!(plot::Violin)
x, y = plot[1], plot[2]
args = @extract plot (width, side, color, show_median, npoints, boundary, bandwidth, weights,
args = @extract plot (width, side, scale, color, show_median, npoints, boundary, bandwidth, weights,

Check warning on line 54 in src/stats/violin.jl

View check run for this annotation

Codecov / codecov/patch

src/stats/violin.jl#L54

Added line #L54 was not covered by tests
datalimits, max_density, dodge, n_dodge, gap, dodge_gap, orientation)
signals = lift(plot, x, y,
args...) do x, y, width, vside, color, show_median, n, bound, bw, w, limits, max_density,
args...) do x, y, width, vside, scale_type, color, show_median, n, bound, bw, w, limits, max_density,
dodge, n_dodge, gap, dodge_gap, orientation
x̂, violinwidth = compute_x_and_width(x, width, gap, dodge, n_dodge, dodge_gap)

Expand Down Expand Up @@ -81,13 +83,20 @@
i1, i2 = searchsortedfirst(k.x, l1), searchsortedlast(k.x, l2)
kde = (x = view(k.x, i1:i2), density = view(k.density, i1:i2))
c = getuniquevalue(color, idxs)
return (x = key.x, side = key.side, color = to_color(c), kde = kde, median = median(v))
return (x = key.x, side = key.side, color = to_color(c), kde = kde, median = median(v), amount = length(idxs))

Check warning on line 86 in src/stats/violin.jl

View check run for this annotation

Codecov / codecov/patch

src/stats/violin.jl#L86

Added line #L86 was not covered by tests
end

(scale_type ∈ [:area, :count, :width]) || error("Invalid scale type: $(scale_type)")

Check warning on line 89 in src/stats/violin.jl

View check run for this annotation

Codecov / codecov/patch

src/stats/violin.jl#L89

Added line #L89 was not covered by tests

max = if max_density === automatic
maximum(specs) do spec
_, max = extrema_nan(spec.kde.density)
return max
if scale_type === :area
return extrema_nan(spec.kde.density) |> last
elseif scale_type === :count
return extrema_nan(spec.kde.density .* spec.amount) |> last
elseif scale_type === :width
return NaN

Check warning on line 98 in src/stats/violin.jl

View check run for this annotation

Codecov / codecov/patch

src/stats/violin.jl#L93-L98

Added lines #L93 - L98 were not covered by tests
end
end
else
max_density
Expand All @@ -98,7 +107,14 @@
colors = RGBA{Float32}[]

for spec in specs
scale = 0.5*violinwidth/max
scale = 0.5 * violinwidth
if scale_type === :area
scale = scale / max
elseif scale_type === :count
scale = scale / max * spec.amount
elseif scale_type === :width
scale = scale / (extrema_nan(spec.kde.density) |> last)

Check warning on line 116 in src/stats/violin.jl

View check run for this annotation

Codecov / codecov/patch

src/stats/violin.jl#L110-L116

Added lines #L110 - L116 were not covered by tests
end
xl = reverse(spec.x .- spec.kde.density .* scale)
xr = spec.x .+ spec.kde.density .* scale
yl = reverse(spec.kde.x)
Expand Down
Loading