From 88ffa6e2bab6665e1cfc88f87817d4e5927b07b5 Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel <22495855+jkrumbiegel@users.noreply.github.com> Date: Mon, 18 Mar 2024 11:57:01 +0100 Subject: [PATCH] Fix contourf level calculation (#3713) * make upper band inclusive for n contourf limits * add reftest * add changelog * length keyword --- CHANGELOG.md | 1 + ReferenceTests/src/tests/examples2d.jl | 12 ++++++++++++ src/basic_recipes/contourf.jl | 6 +++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a508c74730..59637e16540 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Added supported markers hint to unsupported marker warn message [#3666](https://github.com/MakieOrg/Makie.jl/pull/3666). - Fixed bug in CairoMakie line drawing when multiple successive points had the same color [#3712](https://github.com/MakieOrg/Makie.jl/pull/3712). - Remove StableHashTraits in favor of calculating hashes directly with CRC32c [#3667](https://github.com/MakieOrg/Makie.jl/pull/3667). +- Fixed `contourf` bug where n levels would sometimes miss the uppermost value, causing gaps [#3713](https://github.com/MakieOrg/Makie.jl/pull/3713). ## [0.20.8] - 2024-02-22 diff --git a/ReferenceTests/src/tests/examples2d.jl b/ReferenceTests/src/tests/examples2d.jl index 72a8cb6a595..b077a24a7fc 100644 --- a/ReferenceTests/src/tests/examples2d.jl +++ b/ReferenceTests/src/tests/examples2d.jl @@ -1392,3 +1392,15 @@ end ylims!(ax, 0, 1) fig end + +@reference_test "contourf bug #3683" begin + x = y = LinRange(0, 1, 4) + ymin, ymax = 0.4, 0.6 + steepness = 0.1 + f(x, y) = (tanh((y - ymin) / steepness) - tanh((y - ymax) / steepness) - 1) + z = [f(_x, _y) for _x in x, _y in y] + + fig, ax, cof = contourf(x, y, z, levels = 2) + Colorbar(fig[1, 2], cof) + fig +end diff --git a/src/basic_recipes/contourf.jl b/src/basic_recipes/contourf.jl index 89ff2ae4da3..c2a15ddbe34 100644 --- a/src/basic_recipes/contourf.jl +++ b/src/basic_recipes/contourf.jl @@ -48,7 +48,7 @@ end # _computed_extendlow # _computed_extendhigh -_get_isoband_levels(levels::Int, mi, ma) = Float32.(LinRange(mi, ma, levels+1)) +_get_isoband_levels(levels::Int, mi, ma) = collect(range(Float32(mi), nextfloat(Float32(ma)), length = levels+1)) function _get_isoband_levels(levels::AbstractVector{<:Real}, mi, ma) edges = Float32.(levels) @@ -84,12 +84,12 @@ function Makie.plot!(c::Contourf{<:Tuple{<:AbstractVector{<:Real}, <:AbstractVec lowcolor = Observable{RGBAf}() map!(compute_lowcolor, c, lowcolor, c.extendlow, c.colormap) c.attributes[:_computed_extendlow] = lowcolor - is_extended_low = lift(!isnothing, c, lowcolor) + is_extended_low = lift(!isnothing, c, c.extendlow) highcolor = Observable{RGBAf}() map!(compute_highcolor, c, highcolor, c.extendhigh, c.colormap) c.attributes[:_computed_extendhigh] = highcolor - is_extended_high = lift(!isnothing, c, highcolor) + is_extended_high = lift(!isnothing, c, c.extendhigh) PolyType = typeof(Polygon(Point2f[], [Point2f[]])) polys = Observable(PolyType[])