'Python subplots with buttons? Click event?

What I would like to do is plot several signals at once and approve them in the eye, for that I wanted to click on the ones I want to approve and select them.

Make several plots side by side with the subplots, and I wanted to select with clicks only a few to get their index, for that there could be some kind of click event on each one or a button on top of each one, does anyone have any idea how to do this ?

I found examples of adding a button to a single plot, but I wanted an individual button for each plot of a subplot.

An example would be this checkbox on top of a plot:

x = np.linspace(0, 5, 100)
y = np.exp(x)
  
# Plotting the graph for exponential function
def plot(checkbox):
      
    # if checkbox is ticked then scatter
    # plot will be displayed
    if checkbox:
        plt.scatter(x, y, s = 5)
        plt.title('Scatter plot of exponential curve')
      
    # if checkbox is not ticked (by default)
    # line plot will be displayed
    else:
        plt.plot(x, y)
        plt.title('Line plot of exponential curve')
          
# calling the interact function        
interact(plot, checkbox = bool())

Or those yes and no buttons next to a plot:

%matplotlib inline
import ipywidgets as widgets
import matplotlib.pyplot as plt

button1 = widgets.Button(description="Yes")
button2 = widgets.Button(description="No")
out = widgets.Output()

buttons = widgets.VBox(children=[button1, button2])
all_widgets = widgets.HBox(children=[buttons, out])
display(all_widgets)

def logyes(b):
    print("yes")

def logno(b):
    print("no")

button1.on_click(logyes)
button2.on_click(logno)

with out:
    plt.figure(figsize=(3,3))
    plt.plot([1,2],[3,4])
    plt.show()

But I wanted to do this with subplots, several plots side by side and each one with its button to do something, which in this case would be to get its index.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source