Repository: JuliaPlots/Makie.jl (julia)

Issue:
Error on valid data for `hlines!` with log scale
- [x] are you running newest version (version from docs) ?
- [x] can you reproduce the bug with a fresh environment ? (`]activate --temp; add Makie`)
- What platform + GPU are you on?
    - Linux, no GPU

I can make a scatterplot on a log-scale for the following values:
```julia
julia> using GLMakie

julia> fig = Figure(); ax = Axis(fig[1, 1], yscale=log);

julia> scatter!(ax, [0.5, 1, 2], [0.5, 1, 2]); fig
```

However, when I add a `hlines!` to the same plot, at a value already in the plot, I get an `invalid y-limits` error:
```julia
julia> hlines!(ax, [0.5]); fig
ERROR: Found invalid y-limits (-0.6931472f0, 2.0f0) for scale log which is defined on the interval 0.0 .. Inf (open)
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] autolimits(ax::Axis, dim::Int64)
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:967
  [3] yautolimits
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:980 [inlined]
  [4] reset_limits!(ax::Axis; xauto::Bool, yauto::Bool, zauto::Bool)
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:588
  [5] reset_limits!
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:559 [inlined]
  [6] plot!(la::Axis, P::Type{Combined{Makie.hlines}}, attributes::Attributes, args::Vector{Float64}; kw_attributes::@Kwargs{})
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:795
  [7] plot!
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:775 [inlined]
  [8] plot!(P::Type{Combined{Makie.hlines}}, ax::Axis, args::Vector{Float64}; kw_attributes::@Kwargs{})
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:805
  [9] plot!(P::Type{Combined{Makie.hlines}}, ax::Axis, args::Vector{Float64})
    @ Makie ~/.julia/packages/Makie/1hq9u/src/makielayout/blocks/axis.jl:803
 [10] hlines!(::Axis, ::Vararg{Any}; attributes::@Kwargs{})
    @ Makie ~/.julia/packages/MakieCore/eLhyO/src/recipes.jl:39
 [11] hlines!(::Axis, ::Vararg{Any})
    @ Makie ~/.julia/packages/MakieCore/eLhyO/src/recipes.jl:38
 [12] top-level scope
    @ REPL[47]:1
```

The value that causes the issue happens to be `log(0.5)`:
```julia

julia> log(0.5)
-0.6931471805599453
```

It seems like when calculating the limits for the hline in a log-scale plot, something about the order of calculations is wrong.

Sidenote: The horixontal line actually appears correctly in the plot. But in a Pluto notebook, the only output I have access to is the error.

Pull request:
# Description

Fixes #3271

Since we removed the back-and-forth transformation in those plots the child plots end up in a transformed space (i.e. post transform_func). With the default `data_limits` just looking at child plots we end up getting transformed limits as a result. This pr fixes that by adding custom `data_limits` methods. 

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## Checklist

- [x] Added an entry in NEWS.md (for new features and breaking changes)
- [ ] Added or changed relevant sections in the documentation
- [x] Added unit tests for new algorithms, conversion methods, etc.
- [ ] Added reference image tests for new plotting functions, recipes, visual options, etc.

Patch:
diff --git a/NEWS.md b/NEWS.md
index 95da45b9d88..087cbf5309b 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -3,6 +3,7 @@
 ## master
 
 - Fixed a bug with h/vlines and h/vspan not correctly resolving transformations [#3418](https://github.com/MakieOrg/Makie.jl/pull/3418)
+- Fixed a bug with h/vlines and h/vspan returning the wrong limits, causing an error in Axis [#3427](https://github.com/MakieOrg/Makie.jl/pull/3427)
 
 ## 0.20.1
 
diff --git a/src/basic_recipes/hvlines.jl b/src/basic_recipes/hvlines.jl
index b5b6d58605a..a919629d6b6 100644
--- a/src/basic_recipes/hvlines.jl
+++ b/src/basic_recipes/hvlines.jl
@@ -87,3 +87,21 @@ function Makie.plot!(p::Union{HLines, VLines})
     linesegments!(p, line_attributes, points)
     p
 end
+
+function data_limits(p::HLines)
+    scene = parent_scene(p)
+    limits = projview_to_2d_limits(scene.camera.projectionview[])
+    itf = inverse_transform(p.transformation.transform_func[])
+    xmin, xmax = apply_transform.(itf[1], first.(extrema(limits)))
+    ymin, ymax = extrema(p[1][])
+    return Rect3f(Point3f(xmin, ymin, 0), Vec3f(xmax - xmin, ymax - ymin, 0))
+end
+
+function data_limits(p::VLines)
+    scene = parent_scene(p)
+    limits = projview_to_2d_limits(scene.camera.projectionview[])
+    itf = inverse_transform(p.transformation.transform_func[])
+    xmin, xmax = extrema(p[1][])
+    ymin, ymax = apply_transform.(itf[2], getindex.(extrema(limits), 2))
+    return Rect3f(Point3f(xmin, ymin, 0), Vec3f(xmax - xmin, ymax - ymin, 0))
+end
\ No newline at end of file
diff --git a/src/basic_recipes/hvspan.jl b/src/basic_recipes/hvspan.jl
index ebd391b1bc9..cb082347705 100644
--- a/src/basic_recipes/hvspan.jl
+++ b/src/basic_recipes/hvspan.jl
@@ -88,3 +88,24 @@ _apply_x_transform(other, v) = error("x transform not defined for transform func
 _apply_y_transform(t::Tuple, v) = apply_transform(t[2], v)
 _apply_y_transform(other, v) = error("y transform not defined for transform function $(typeof(other))")
 _apply_y_transform(::typeof(identity), v) = v
+
+
+function data_limits(p::HSpan)
+    scene = parent_scene(p)
+    limits = projview_to_2d_limits(scene.camera.projectionview[])
+    itf = inverse_transform(p.transformation.transform_func[])
+    xmin, xmax = apply_transform.(itf[1], first.(extrema(limits)))
+    ymin = minimum(p[1][])
+    ymax = maximum(p[2][])
+    return Rect3f(Point3f(xmin, ymin, 0), Vec3f(xmax - xmin, ymax - ymin, 0))
+end
+
+function data_limits(p::VSpan)
+    scene = parent_scene(p)
+    limits = projview_to_2d_limits(scene.camera.projectionview[])
+    itf = inverse_transform(p.transformation.transform_func[])
+    xmin = minimum(p[1][])
+    xmax = maximum(p[2][])
+    ymin, ymax = apply_transform.(itf[2], getindex.(extrema(limits), 2))
+    return Rect3f(Point3f(xmin, ymin, 0), Vec3f(xmax - xmin, ymax - ymin, 0))
+end
\ No newline at end of file


Test patch:
diff --git a/test/boundingboxes.jl b/test/boundingboxes.jl
index 37aa126331e..e7a83fb34fd 100644
--- a/test/boundingboxes.jl
+++ b/test/boundingboxes.jl
@@ -15,6 +15,23 @@ end
 
     fig, ax, p = bracket(ps...)
     @test data_limits(p) ≈ Rect3f(Point3f(0), Vec3f(1, 1, 0))
+
+    fig = Figure()
+    ax = Axis(fig[1, 1], yscale=log, xscale=log)
+    scatter!(ax, [0.5, 1, 2], [0.5, 1, 2])
+    p1 = vlines!(ax, [0.5])
+    p2 = hlines!(ax, [0.5])
+    p3 = vspan!(ax, [0.25], [0.75])
+    p4 = hspan!(ax, [0.25], [0.75])
+    Makie.reset_limits!(ax)
+
+    lims = ax.finallimits[]
+    x, y = minimum(lims); w, h = widths(lims)
+
+    @test data_limits(p1) ≈ Rect3f(Point3f(0.5, y, 0), Vec3f(0, h, 0))
+    @test data_limits(p2) ≈ Rect3f(Point3f(x, 0.5, 0), Vec3f(w, 0, 0))
+    @test data_limits(p3) ≈ Rect3f(Point3f(0.25, y, 0), Vec3f(0.5, h, 0))
+    @test data_limits(p4) ≈ Rect3f(Point3f(x, 0.25, 0), Vec3f(w, 0.5, 0))
 end
 
 @testset "boundingbox(plot)" begin