well, after a few tries I did it.
attaching the code:
### imports ###
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from mpl_toolkits.mplot3d import Axes3D
### general defines ###
# assuming N,M are odd
N = 9
xs = np.linspace(1 , N , N , endpoint=True)
ys = np.linspace(1 , N , N , endpoint=True)
xx , yy = np.meshgrid(xs , ys)
data = np.concatenate((xx.reshape(N*N,1), yy.reshape(N*N,1)), axis=1)
### classes ###
class Index(object):
def push_button(self, event):
arr = np.flip(dynamicArr,0)
figi = plt.figure()
axi = figi.add_subplot(1,1,1,projection='3d')
axi.plot_surface(xx,yy,arr)
figi.show()
### main ###
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
ax.set_title('click on array element to select/remove it')
coll = ax.scatter(data[:, 0] , data[:, 1], color=["blue"]*N*N, picker = 5, s=[150]*N*N)
coll._facecolors[:, :] = (190, 63, 63, 1)
coll._edgecolors[:, :] = (190, 63, 63, 1)
ax.axis([0, N+1, 0, N+1])
dynamicArr = np.ones((N,N) , dtype=int)
def on_pick(event):
row = int(N-1 - int(event.ind/N))
col = int(event.ind%N)
if dynamicArr[row, col] == 0:
dynamicArr[row, col] = 1
coll._facecolors[event.ind, :] = (190, 63, 63, 1)
coll._edgecolors[event.ind, :] = (190, 63, 63, 1)
else:
dynamicArr[row, col] = 0
coll._facecolors[event.ind, :] = (100, 100, 100, 100) #(63 , 190 , 190 , 1)
coll._edgecolors[event.ind, :] = (100, 100, 100, 100) #(63 , 190 , 190 , 1)
fig.canvas.draw()
# adding callback button to figure
callback = Index()
axdraw = plt.axes([0.395, 0.05, 0.25, 0.075])
bdraw = Button(axdraw, 'push button')
bdraw.on_clicked(callback.push_button)
# enabling interactive GUI
fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()