# How do I select a line by clicking in Makie?

**URL:** <https://discourse.julialang.org/t/how-do-i-select-a-line-by-clicking-in-makie/93410>\
**Category:** Visualization\
**Tags:** interactivity\
**Created:** [January 23, 2023, 1:59pm UTC](https://discourse.julialang.org/t/how-do-i-select-a-line-by-clicking-in-makie/93410 "2023-01-23T13:59:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladdez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vladdez/32/45806_2.png) [@vladdez](https://discourse.julialang.org/u/vladdez)\
**Post date:** [January 23, 2023, 1:59pm UTC](https://discourse.julialang.org/t/how-do-i-select-a-line-by-clicking-in-makie/93410/1 "2023-01-23T13:59:36Z")

</div>

I want to be able to select a line on the plot by clicking on it. Ideally, when I click on any line, the number of that line will appear on the screen. I wrote my code based on the tutorial, but there is no example with lines, so I did what I did. Tutorial: [Page Redirection](https://docs.makie.org/v0.19/documentation/events/index.html#point_picking)

At the moment I have no idea what these numbers are telling me and why. They are not even coordinates of clicked points.

P.S. Actually it is just a starting point. I want to create event interaction on series and topoplots. But for now it would be great to find out the basics.

```julia
f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), resolution = (1500, 700))
ax = Axis(f[1, 1], xlabel = "Time [s]", ylabel = "Voltage amplitude [µV]") 

N = 1:length(pos)
positions = Observable(rand(Point2f, 10))

xs = 0:0.01:10
ys = 0.5 .* sin.(xs)

lines!(xs, ys)
lines!(xs, ys * 2)

hidedecorations!(ax, label = false, ticks = false, ticklabels = false) 
hidespines!(ax, :t, :r) 
hlines!(0, color = :gray, linewidth = 1)
vlines!(0, color = :gray, linewidth = 1)

i = Observable(0)
on(events(f).mousebutton, priority = 2) do event
    if event.button == Mouse.left && event.action == Mouse.press
        plt, i[] = pick(f)
        str = lift(i -> "$(i)", i)
        text!(ax, 1, -0.5, text = str, align = (:center, :center)) 
    end
end
f

```

Below are some examples of the interaction between clicking and the number displayed (the red dot is where I click).

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d9dad9b7418f819221b662798c03ba7328f7016.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/849d26994bd6e7fe07737a311a48f4e1d03f181d.png)

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 23, 2023, 4:37pm UTC](https://discourse.julialang.org/t/how-do-i-select-a-line-by-clicking-in-makie/93410/2 "2023-01-23T16:37:34Z")

</div>

From the docs you linked

```julia
 For lines and linesegments it's end position of the clicked line segment.

```

So to get the coordinates, you need to first ensure `plt` is one of the two lines objects you created at lines 10 and 11 of your script, and then index into the `xs` and `ys` arrays using `i`.

---

<div class="post-metadata">

**Author:** ![vladdez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vladdez/32/45806_2.png) [@vladdez](https://discourse.julialang.org/u/vladdez)\
**Post date:** [January 23, 2023, 4:52pm UTC](https://discourse.julialang.org/t/how-do-i-select-a-line-by-clicking-in-makie/93410/3 "2023-01-23T16:52:16Z")

</div>

It is not clear to me what the end position of the clicked line segment is. What is the line segment, what is the end position?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 23, 2023, 5:26pm UTC](https://discourse.julialang.org/t/how-do-i-select-a-line-by-clicking-in-makie/93410/4 "2023-01-23T17:26:11Z")

</div>

Does this help:

```julia
using GLMakie

function picklines()
    f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), resolution = (1500, 700))
    ax = Axis(f[1, 1], xlabel = "Time [s]", ylabel = "Voltage amplitude [µV]")

    #N = 1:length(pos)
    positions = Observable(rand(Point2f, 10))

    xs = 0:0.01:10
    ys = 0.5 .* sin.(xs)

    lns = lines!(xs, ys)
    lns2 = lines!(xs, ys * 2)

    hidedecorations!(ax, label = false, ticks = false, ticklabels = false)
    hidespines!(ax, :t, :r)
    hlines!(0, color = :gray, linewidth = 1)
    vlines!(0, color = :gray, linewidth = 1)

    i = Observable((0.0,0.0))
    str = lift(i -> "$(i)", i)
    text!(ax, 1, -0.5, text = str, align = (:center, :center))
    on(events(f).mousebutton, priority = 2) do event
        if event.button == Mouse.left && event.action == Mouse.press
            plt, p = pick(f)
            if plt == lns
                i[] = (xs[p], ys[p])
            elseif plt == lns2
                i[] = (xs[p], 2*ys[p])
            end
        end
    end
    f
end

```

---

<div class="post-metadata">

**Author:** ![vladdez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vladdez/32/45806_2.png) [@vladdez](https://discourse.julialang.org/u/vladdez)\
**Post date:** [January 24, 2023, 1:48pm UTC](https://discourse.julialang.org/t/how-do-i-select-a-line-by-clicking-in-makie/93410/5 "2023-01-24T13:48:29Z")

</div>

Thank you! This helps to get coordinates for lines, but unfortunately doesn’t tell you which line was selected. But that’s helpful too, it perfectly works for topoplots!
