Mpl_point_clicker: how to return to code after clicking?

In the simplest example of using mpl_point_clicker, points are obtained by clicking on an image. However, the resulting array when printed is empty, because the code is executed before I can click on the image.

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('QT5Agg')                                   
import numpy as np
from mpl_point_clicker import clicker # https://mpl-point-clicker.readthedocs.io/en/latest/
 
empty_plot          = np.zeros((10,10))         

fig, ax             = plt.subplots() 
plot1               = ax.imshow(empty_plot)    
klicker             = clicker(ax,['point'],markers=['x'])                 
plt.show()

print(klicker.get_positions())    


The above code results in the following:
{'point': array([], dtype=float64)}

This seems to be a common issue for Python: https://stackoverflow.com/questions/17149646/matplotlib-force-plot-display-and-then-return-to-main-code, https://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue

Unfortunately none of the proposed solutions in those links work for me. I’m working with several hundred images, looking for specific features in them. I’m trying to get XY positions of the features, add that information to a dataframe, and then plot the next set of images to repeat the process.

Since I’d imagine that anybody using or developing mpl_point_clicker might also want to use point position information in their code, I’m hoping there might be a known way to work around this problem.

Thanks!

What context are you doing this? I.e. how are you running this code, in a script, in ipython some other way?

If you then want to use the (x,y) coordinates you simply call the array as

vertices1 = (klicker.get_positions())['event']

But, watch out! This is an array, and depending on what you need this for, you might want to convert the array to a list:

vertices1 = reg_coo.flatten()
vertices2 = []
[vertices2.append(element) for element in vertices1]

This way you can use it to draw a region.


Polygon2  = Polygon_Area(vertices2)
patch = patches.PathPatch(Polygon2, facecolor='none', lw=2, ls=':')
ax110.add_patch(patch)