# Makie: mouse position in data coordinates

**URL:** <https://discourse.julialang.org/t/makie-mouse-position-in-data-coordinates/67573>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [September 2, 2021, 1:30pm UTC](https://discourse.julialang.org/t/makie-mouse-position-in-data-coordinates/67573 "2021-09-02T13:30:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![inverseproblem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/inverseproblem/32/5346_2.png) [@inverseproblem](https://discourse.julialang.org/u/inverseproblem)\
**Post date:** [September 2, 2021, 1:30pm UTC](https://discourse.julialang.org/t/makie-mouse-position-in-data-coordinates/67573/1 "2021-09-02T13:30:39Z")

</div>

Hello,

I’m having troubles in getting the mouse position in data coordinates from a Makie plot.  
Using `mouseposition()` seems to return the position in pixel coordinates contrary to what described in the documentation [http://makie.juliaplots.org/v0.15.2/documentation/api\_reference/index.html#Events](http://makie.juliaplots.org/v0.15.2/documentation/api_reference/index.html#Events). Maybe be related to the function `to_world()`.

Here below a MWE, where `mouseposition()` returns the same result that `mouseposition_px()` and `to_world(fig.scene,Point2f(pos_px))`.

What am I doing wrong?

```julia
using GLMakie

img = rand(100, 100)

fig = Figure()
ax1 = Axis(fig[1,1])
image!(ax1,img) 

on(events(fig.scene).mousebutton, priority = 0) do event
     if event.button == Mouse.left
         if event.action == Mouse.press
             pos = mouseposition(fig.scene)
             pos_px = Makie.mouseposition_px(fig.scene)
             twpos = to_world(fig.scene,Point2f(pos_px))
             println("mouseposition(): $pos, mouseposition_px(): $pos_px, to_world(pos): $twpos")
             println()
         end
     end
    return Consume(false)
end

fig

```

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [September 2, 2021, 1:59pm UTC](https://discourse.julialang.org/t/makie-mouse-position-in-data-coordinates/67573/2 "2021-09-02T13:59:10Z")

</div>

One option is to use the higher level interaction interface of the axis:

```julia
register_interaction!(ax1, :my_interaction) do event::MouseEvent, axis
    if event.type === MouseEventTypes.leftclick
        println("You clicked on the axis at datapos $(event.data)")
    end
end

```

see [this section](http://makie.juliaplots.org/v0.15.2/examples/layoutables/axis/index.html#custom_interactions) in the docs.

---

<div class="post-metadata">

**Author:** ![inverseproblem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/inverseproblem/32/5346_2.png) [@inverseproblem](https://discourse.julialang.org/u/inverseproblem)\
**Post date:** [September 2, 2021, 3:49pm UTC](https://discourse.julialang.org/t/makie-mouse-position-in-data-coordinates/67573/3 "2021-09-02T15:49:21Z")

</div>

Great suggestion, thanks a lot!

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [September 3, 2021, 12:57am UTC](https://discourse.julialang.org/t/makie-mouse-position-in-data-coordinates/67573/4 "2021-09-03T00:57:54Z")

</div>

The reason why `mouseposition(fig.scene)` is giving you pixel coordinates is that `fig.scene` is in pixel coordinates. `mouseposition(ax.scene)` should give you axis coordinates.

---

<div class="post-metadata">

**Author:** ![inverseproblem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/inverseproblem/32/5346_2.png) [@inverseproblem](https://discourse.julialang.org/u/inverseproblem)\
**Post date:** [September 3, 2021, 5:43pm UTC](https://discourse.julialang.org/t/makie-mouse-position-in-data-coordinates/67573/5 "2021-09-03T17:43:51Z")

</div>

That explains why I was not getting the position in data coordinates… Thank you!
