Is there a way to track the number of clicks made when using matplotlib's ginput() function?

45 views Asked by At

Here is the documentation for the ginput() function. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.ginput.html

I know that the number of clicks is kept track of internally, because the first parameter for the function is n: int, default: 1, Number of mouse clicks to accumulate. If negative, accumulate clicks until the input is terminated manually.

Is there a way to have this number saved in a variable? Here is my current working code:

# enable the user to click to add labels, store the clicks as a list of tuples called pts
    tellme('You will define target labels, click to begin')
    plt.waitforbuttonpress()
    
    while True:
        pts = []
        tellme('Select target locations with mouse. Right click to remove most recently added point. When finished, use middle button to terminate input.')
        pts = np.asarray(plt.ginput(-1, timeout=-1)) #ginput returns a list of the clicked (x, y) coordinates (a list of tuples)
        

        print(f'Selected points: {pts}')
  

        # is there a way to track direct middle click vs middle click after right click? that would be a solution to the one edge case 

        tellme('Happy? Press Enter for yes, mouse click for no')

        if plt.waitforbuttonpress():
            # if pts is empty, then use the raw labels as the clean label
            if len(pts) == 0 & len(scan.raw_labels_polar) > 1:
                pts = scan.raw_labels_polar
            #if pts is empty, but not because user wants to use original labels, but rather just generate an empty csv
            if len(pts) == 0 & len(scan.raw_labels_polar) == 1 & **num_clicks** > 0:
                pts = []
            
            labels.list_to_csv(pts, scan.scan_number, save_to)  # save the new list of tuples as a csv in path

            break
1

There are 1 answers

0
Ratislaus On

With matplotlib.backend_bases.MouseEvent you could try to count click events in a dedicated handler, for example like this:

import matplotlib.pyplot as plt
from matplotlib.backend_bases import MouseButton

# mouse clicks counters
left_count = 0
right_count = 0
middle_count = 0

# handler called on mouse button press events 
def on_press(event):
    print('you pressed', event.button, event.xdata, event.ydata)
    if event.button == MouseButton.LEFT:
        global left_count
        left_count = left_count + 1
    elif event.button == MouseButton.RIGHT:
        global right_count
        right_count = right_count + 1
    elif event.button == MouseButton.MIDDLE:
        global middle_count
        middle_count = middle_count + 1
    else:
        print('you pressed something else')

# figure associated with that handler
fig, ax = plt.subplots()
ax.set_title('click on me')
fig.canvas.mpl_connect('button_press_event', on_press)
plt.show()

# show counter values
print('left: ', left_count)
print('right: ', right_count)
print('middle: ', middle_count)