I am using python 2.7 on Windows. I have a function which creates a figure with a CheckButtons widget, and it also includes the definition of the button's callback. When I call the function once, everything is OK, but when I call it more than once, the buttons stops responding, as follows:
- If the figure is created using
plt.subplots(), none of the buttons respond. - If the figure was created using
plt.figure(), the behavior is inconsistent; sometimes only the 1st created button responds, and sometimes both respond.
My guess is that is has to do with the scope of the callback, but I couldn't pinpoint the problem using trial-and-error.
Sample code:
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
def create_button():
plt.subplots() # or: plt.figure()
rax = plt.axes([0.2, 0.2, 0.2, 0.2])
check = CheckButtons(rax, ['on'], [True])
def callback(label):
check.labels[0].set_text('on' if check.lines[0][0].get_visible() else 'off')
plt.draw()
check.on_clicked(callback)
create_button()
#create_button() # uncomment to reproduce problem
plt.show()