Masquer une courbe sur une figure avec tkinter
Bonjour à tous,
Mon problème est le suivant :
J'ai une figure avec un plot de deux courbes : exp et sin. J'aimerais afficher cette figure dans une fenêtre tkinter afin d'insérer un widget du type checkbutton qui permettrait d'afficher ou non la courbe du sinus.
Je me suis rendu compte en cherchant que c'est un sujet très utile et pourtant mal renseigné, j'espère que cet exemple simple aidera du monde. J'en appelle à votre pédagogie, mes connaissances en programmation sont limitées.
voici l'ébauche qui ne fonctionne pas :
Code:
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import numpy as np
import matplotlib.pyplot as plt
from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
class BuckysButtons:
def __init__(self,master):
frame = Frame(master)
frame.pack()
self.printButton = Checkbutton(frame, text="Sinus", command=self.plotsin)
self.printButton.pack(side=LEFT)
self.quitButton = Button(frame, text="Quit", command= frame.quit)
self.quitButton.pack(side=LEFT)
def plotsin(self):
ax2 = ax1.twinx()
s2 = np.sin(2 * np.pi * t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin', color='r')
ax2.tick_params('y', colors='r')
root = Tk()
b= BuckysButtons(root)
fig=plt.figure()
ax1=fig.add_subplot(111)
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color='b')
ax1.tick_params('y', colors='b')
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
root.mainloop() |
Merci beaucoup pour toute réponse constructive!