1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
fig, ax = plt.subplots(figsize=(6.5, 6))
fig.subplots_adjust(left=0.2)
# plot
linelist=[]
x = [0, 1, 2]
y = [0, 1, 3]
lines = plt.plot(x, y, lw=5, visible=True)
linelist.append(lines)
lines = plt.plot(y, y, lw=5, visible=True)
linelist.append(lines)
lines = plt.plot(y, x, lw=5, visible=True)
linelist.append(lines)
ax.set_ylim(-1.5, 1.5)
# check button
rax = plt.axes([0.02, 0.4, 0.13, 0.2], aspect='equal')
labels = ['var1', 'var2', 'var3']
state = [True, True, True]
check = CheckButtons(rax, labels, state)
def func(label):
i = labels.index(label)
linelist[i][0].set_visible(not linelist[i][0].get_visible())
fig.canvas.draw()
check.on_clicked(func)
plt.show() |
Partager