MakieOrg / Makie.jl

Interactive data visualizations and plotting in Julia
https://docs.makie.org/stable
MIT License
2.41k stars 312 forks source link

heatmap and pick #3265

Closed bjarthur closed 3 weeks ago

bjarthur commented 1 year ago

picking on a heatmap always returns 0 no matter what square was clicked on:

julia> fig, ax, hm = heatmap(rand(3,3))
Screenshot 2023-09-28 at 9 43 56 AM
julia> on(events(fig).mousebutton, priority = 2) do event
           if event.button == Mouse.left && event.action == Mouse.press
               plt, i = pick(fig)
               @info plt, i
           end
           return Consume(false)
       end
ObserverFunction defined at REPL[41]:2 operating on Observable(MouseButtonEvent(none, release))

julia> [ Info: (nothing, 0)
[ Info: (Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, 0)
[ Info: (Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, 0)
[ Info: (Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, 0)
[ Info: (Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, 0)
[ Info: (Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, 0)
[ Info: (Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, 0)
[ Info: (Heatmap{Tuple{Vector{Float32}, Vector{Float32}, Matrix{Float32}}}, 0)

would be super useful if this returned a cartesian or linear index, much like it does for scatter:

julia> fig, ax, li = scatter([1,2,3],[1,2,3])

julia> on(events(fig).mousebutton, priority = 2) do event
           if event.button == Mouse.left && event.action == Mouse.press
               plt, i = pick(fig)
               @info plt, i
           end
           return Consume(false)
       end
ObserverFunction defined at REPL[43]:2 operating on Observable(MouseButtonEvent(none, release))

julia> [ Info: (Scatter{Tuple{Vector{Point{2, Float32}}}}, 3)
[ Info: (Scatter{Tuple{Vector{Point{2, Float32}}}}, 2)
[ Info: (Scatter{Tuple{Vector{Point{2, Float32}}}}, 1)
julia> VERSION
v"1.9.3"

julia> Sys.MACHINE
"arm64-apple-darwin22.4.0"

  [e9467ef8] GLMakie v0.8.10
bjarthur commented 1 year ago

so just to be clear, i know that this is documented not to be supported, just wondering why. is there a suggested work around, like maybe manually drawing a bunch of squares?

bjarthur commented 1 year ago

here's a workaround:

on(events(fig).mousebutton, priority = 2) do event
    if event.button == Mouse.left && event.action == Mouse.release
        xy = mouseposition(ax)  # data coords
        i,j = round.(xy)   # grid coords
        1<=i<=3 && 1<=j<=3 && @info i,j
    end
    return Consume(false)
end