Bonjour, j'ai 2 problèmes avec mon code:

Je dispose d'une fenêtre tkinter avec 2 graphes sous matplotlib

1) J'aimerais avoir une seul toolbar pour mes 2 graphes es ce possible ?

2) J'aimerais pouvoir afficher/masquer une courbe quand on clique sur une checkbox, j'arrive actuellement juste a la masquer, mais pas a la re-afficher. Mais je pense pas que j'utilise la bonne commande ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import sys
from tkinter import *
import tkinter as Tk
 
root = Tk.Tk()
root.wm_title("Embedding in TK")
 
#figure1
 
f1 = Figure(figsize=(5,4), dpi=100)
a1 = f1.add_subplot(111)
 
x = [0, 1, 2]
y = [0, 1, 3] 
 
 
lines = a1.plot(x, y, lw=5, visible=True)
lines = a1.plot(y, x, lw=5, visible=True)
 
 
canvas = FigureCanvasTkAgg(f1, master=root)
canvas.show()
 
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
 
 
toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
 
 
#figure2
 
f2 = Figure(figsize=(5,4), dpi=100)
a2 = f2.add_subplot(111)
 
x = [0, 1, 2]
y = [0, -1, -3] 
lines2 = a2.plot(x, y, lw=5, visible=True)
 
 
canvas2 = FigureCanvasTkAgg(f2, master=root)
canvas2.show()
canvas2.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
 
toolbar = NavigationToolbar2TkAgg( canvas2, root )
toolbar.update()
canvas2._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
 
 
def on_key_event(event):
    print('you pressed %s'%event.key)
    key_press_handler(event, canvas, toolbar)
 
canvas.mpl_connect('key_press_event', on_key_event)
def _quit():
    root.quit()
    root.destroy()
 
def hideline():
    lines.pop(0).remove()
    canvas.show()
 
button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)
 
option = Frame(root)
option.pack()
 
var = IntVar()
var2 = IntVar()
c = Checkbutton(option, text="courbe 1", variable=var, command=hideline)
c.pack()
c = Checkbutton(option, text="courbe 2", variable=var2, command=hideline)
c.pack()
 
Tk.mainloop()
Merci d'avance pour votre aide.