# Storing mouse click event coordinates in PyPlot window

**URL:** <https://discourse.julialang.org/t/storing-mouse-click-event-coordinates-in-pyplot-window/21117>\
**Category:** Visualization\
**Created:** [February 23, 2019, 5:32pm UTC](https://discourse.julialang.org/t/storing-mouse-click-event-coordinates-in-pyplot-window/21117 "2019-02-23T17:32:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carsten](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Carsten](https://discourse.julialang.org/u/Carsten)\
**Post date:** [February 23, 2019, 5:32pm UTC](https://discourse.julialang.org/t/storing-mouse-click-event-coordinates-in-pyplot-window/21117/1 "2019-02-23T17:32:13Z")

</div>

I want to get access to the cursor positions within a PyPlot window and store all clicked values into arrays. I was able to print out the x,y values but I failed to store them into arrays.

The code first I used is:

```julia
function test(event)
    println("Click: $(event[:xdata]), $(event[:ydata])")
end

fig = PyPlot.figure()
PyPlot.plot(0:1:10, 0:1:10)
cid = fig[:canvas][:mpl_connect]("button_press_event", test)

```

Running this small code snippet the x,y coordinates were printed as expected. However when I try to store the values into arrays applying the following code:

```julia
x = Array{Float64,1}(undef,1); x = fill!(x,0)
y = Array{Float64,1}(undef,1); y = fill!(y,0)
function test(event)
    println("Click: $(event[:xdata]), $(event[:ydata])")
    x = push!(x,event[:xdata])
    y = push!(y,event[:xdata])
end

fig = PyPlot.figure()
PyPlot.plot(0:1:10, 0:1:10)
cid = fig[:canvas][:mpl_connect]("button_press_event", test)

```

I got the follwing error code:

```julia
julia> Click: 4.622983870967742, 4.563359234907353
Traceback (most recent call last):
  File "C:\Users\Carsten\.julia\conda\3\lib\site-packages\matplotlib\cbook\ __init__.py", line 215, in process
    func(*args, **kwargs)
RuntimeError: Julia exception: UndefVarError(:x)

```

How can I store the clicked values for further use?  
Many thanks in advanced

---

<div class="post-metadata">

**Author:** ![hckr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hckr/32/4836_2.png) [@hckr](https://discourse.julialang.org/u/hckr)\
**Post date:** [February 23, 2019, 9:01pm UTC](https://discourse.julialang.org/t/storing-mouse-click-event-coordinates-in-pyplot-window/21117/2 "2019-02-23T21:01:58Z")

</div>

It is [the famous global scope issue](https://docs.julialang.org/en/v1/manual/variables-and-scoping/index.html#Local-Scope-1) although it seems to be created unnecessarily here.

To assign a global variable within a local scope you need to add the keyword `global` before the assignments. However, you do not need the assignments after the calls to `push!`. Therefore simply replacing

```julia
    x = push!(x,event[:xdata])
    y = push!(y,event[:xdata])

```

with

```julia
    push!(x,event[:xdata])
    push!(y,event[:xdata])

```

will make your code work as expected.

In other cases, you either need to use the `global` keyword or take the global variables as parameters to your functions.
