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

Fix violin indexing error #4682

Merged
merged 4 commits into from
Jan 2, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Fixed indexing error edge case in violin median code [#4682](https://github.com/MakieOrg/Makie.jl/pull/4682)

## [0.22.0] - 2024-12-12

- Updated to GeometryBasics 0.5: [GeometryBasics#173](https://github.com/JuliaGeometry/GeometryBasics.jl/pull/173), [GeometryBasics#219](https://github.com/JuliaGeometry/GeometryBasics.jl/pull/219) [#4319](https://github.com/MakieOrg/Makie.jl/pull/4319)
Expand Down
6 changes: 3 additions & 3 deletions src/stats/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ function plot!(plot::Violin)
if show_median
# interpolate median bounds between corresponding points
xm = spec.median
ip = findfirst(>(xm), spec.kde.x)
ym₋, ym₊ = spec.kde.density[ip-1], spec.kde.density[ip]
xm₋, xm₊ = spec.kde.x[ip-1], spec.kde.x[ip]
ip = Base.max(2, something(findfirst(>(xm), spec.kde.x), length(spec.kde.x)))
ym₋, ym₊ = spec.kde.density[Base.max(1, ip-1)], spec.kde.density[ip]
xm₋, xm₊ = spec.kde.x[Base.max(1, ip-1)], spec.kde.x[ip]
ym = (xm * (ym₊ - ym₋) + xm₊ * ym₋ - xm₋ * ym₊) / (xm₊ - xm₋)
median_left = point_func(spec.side == 1 ? spec.x : spec.x - ym * scale, xm)
median_right = point_func(spec.side == -1 ? spec.x : spec.x + ym * scale, xm)
Expand Down
6 changes: 6 additions & 0 deletions test/statistical_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,10 @@ end
@test p2.plots[2] isa LineSegments
@test p2.plots[2][:color][] === :white
@test p2.plots[2][:visible][] === :false

# median edge case #4675
@test violin(fill(1, 1000), push!(fill(0, 999), 1), show_median=true, datalimits=(-0.001,Inf)) !== nothing
# And some others
@test violin(fill(1, 1000),fill(0, 1000), show_median=true) !== nothing
@test violin([1], [1], show_median=true) !== nothing
end
Loading