# Add points to scatter plot by Mouse.down click using GLMakie

**URL:** https://discourse.julialang.org/t/add-points-to-scatter-plot-by-mouse-down-click-using-glmakie/89740
**Category:** Visualization
**Tags:** question, plotting, makie, glmakie
**Created:** [November 3, 2022, 9:56pm UTC](https://discourse.julialang.org/t/add-points-to-scatter-plot-by-mouse-down-click-using-glmakie/89740 "2022-11-03T21:56:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mgalenb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgalenb/32/5645_2.png) [@mgalenb](https://discourse.julialang.org/u/mgalenb)
#### Post date: [November 3, 2022, 9:56pm UTC](https://discourse.julialang.org/t/add-points-to-scatter-plot-by-mouse-down-click-using-glmakie/89740/1 "2022-11-03T21:56:52Z")

</div>

Id like to add points to a GLMakie plot when holding down the keyboard letter “a” and then holding down the left mouse button. The docs have a good demo on how to do this with a click ([Point Picking](https://docs.makie.org/v0.18.2/documentation/events/index.html#point_picking)). However when I change `Mouse.press` to `Mouse.down` the zoom action occurs. Any idea how to turn off the zoom action?

Here is my code:

```julia
using GLMakie

positions = Observable(rand(Point2f, 10))

fig, ax, p = scatter(positions)

on(events(fig).mousebutton, priority = 2) do event
    if event.button == Mouse.left && event.action == Mouse.down
        if Keyboard.d in events(fig).keyboardstate
            # Delete marker
            plt, i = pick(fig)
            if plt == p
                deleteat!(positions[], i)
                notify(positions)
                return Consume(true)
            end
        elseif Keyboard.a in events(fig).keyboardstate
            # Add marker
            push!(positions[], mouseposition(ax))
            notify(positions)
            return Consume(true)
        end
    end
    return Consume(false)
end

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 3, 2022, 10:26pm UTC](https://discourse.julialang.org/t/add-points-to-scatter-plot-by-mouse-down-click-using-glmakie/89740/2 "2022-11-03T22:26:52Z")

</div>

[https://docs.makie.org/stable/examples/blocks/axis/#registering\_and\_deregistering\_interactions](https://docs.makie.org/stable/examples/blocks/axis/#registering_and_deregistering_interactions)

---

<div class="post-metadata">

### Author: ![mgalenb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgalenb/32/5645_2.png) [@mgalenb](https://discourse.julialang.org/u/mgalenb)
#### Post date: [November 4, 2022, 5:15am UTC](https://discourse.julialang.org/t/add-points-to-scatter-plot-by-mouse-down-click-using-glmakie/89740/3 "2022-11-04T05:15:15Z")

</div>

Thanks @jules . I have another question. id like continually add new points to the plot when the mouse button is pressed. Basically I am trying to paint over pixels in a 2D image. I have tried change Mouse.press to Mouse.pressed but that doesn’t seem to work.

---

<div class="post-metadata">

### Author: ![mgalenb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgalenb/32/5645_2.png) [@mgalenb](https://discourse.julialang.org/u/mgalenb)
#### Post date: [November 4, 2022, 7:10am UTC](https://discourse.julialang.org/t/add-points-to-scatter-plot-by-mouse-down-click-using-glmakie/89740/4 "2022-11-04T07:10:05Z")

</div>

Update: Look at the dragging example in the docs, duh. [Point Picking](https://docs.makie.org/v0.18.2/documentation/events/index.html#point_picking)

Changed the bottom dragging function to this,

```julia
    if dragging
        #positions[][idx] = mouseposition(ax)
        push!(positions[], mouseposition(ax))
        notify(positions)
        return Consume(true)
    end
    return Consume(false)
end

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 4, 2022, 8:51am UTC](https://discourse.julialang.org/t/add-points-to-scatter-plot-by-mouse-down-click-using-glmakie/89740/5 "2022-11-04T08:51:21Z")

</div>

I would not use `dragging` state directly, because this will still fire if you drag the mouse over the axis from somewhere else. This often happens with sliders etc. You can just use the precomputed mouse events accessible via the axis interactions as explained here [https://docs.makie.org/stable/examples/blocks/axis/#function\_interaction](https://docs.makie.org/stable/examples/blocks/axis/#function_interaction)

For example (I didn’t test this)

```julia
register_interaction!(ax, :painting_scatters) do event::MouseEvent, axis
    if event.type === MouseEventTypes.leftdrag
        # do your picking logic etc here
    end
end

```
