Я пытаюсь написать фрагмент кода, который будет запускать событие нажатия кнопки, только если установлен флажок.
Более подробно, событие нажатия кнопки записывает координаты щелчка мышии строит вертикальную красную линию на графике в координатах, по которым щелкнули.Тем не менее, я хочу, чтобы он запускался только в том случае, если установлен флажок «Вкл.».
Может ли кто-нибудь помочь мне исправить следующий код?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
import ipywidgets as widgets
from ipywidgets import interactive
#mouse click function to store coordinates and plot vertical red line
def onclick(event):
if event == 'On':
global ix,iy
ix, iy = event.xdata, event.ydata
global click_count
click_count.append((ix,iy))
#plotting lines where clicks occur
if len(click_count) > 1:
ax1.axvline(x=ix, ymin=0, ymax=1, color = "red")
# assign global variable to access outside of function
global coords3
coords3.append((ix, iy))
# Disconnect after 12 clicks
if len(coords3) == 12:
fig.canvas.mpl_disconnect(cid)
plt.close(1)
return
#check box function
def func(label):
if label == 'On':
return 'On'
#plots the graph
x = range(0,10)
y = range(0,10)
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box
ax2 = plt.axes([0.7, 0.05, 0.1, 0.075])
check_box= CheckButtons(ax2, ('On', 'Off'), (False, False))
#define check box function
check = check_box.on_clicked(func)
# calling out the click coordinates to a variable
coords3 = []
click_count = []
# Call click func
cid = fig.canvas.mpl_connect('button_press_event', onclick(check))
plt.show(1)