I have a function fun() that binds a callback callback() when a matplotlib figure is clicked. I want this callback to be able to access the variable space of fun() to make changes. How can I go about this?
import numpy as np
import matplotlib.pyplot as plt
def callback(event):
data = event.xdata
def fun():
data = 0
fig, ax = plt.subplots()
ax.plot(np.random.rand(12), np.random.rand(12), 'go')
fig.canvas.mpl_connect('button_release_event', callback)
plt.show()
return data
print fun()
Putting data in the global scope is not an acceptable solution. The function should be self-contained. The answer here would allow me to pass variables to callback(), but would not let me edit them.