It is the famous global scope issue 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
x = push!(x,event[:xdata])
y = push!(y,event[:xdata])
with
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.