forked from furas/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (59 loc) · 1.94 KB
/
Copy pathmain.py
File metadata and controls
86 lines (59 loc) · 1.94 KB
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
84
85
#!/usr/bin/env python3
#
# http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html
#
# update: 2019.08.03
#
# NavigationToolbar2TkAgg is deprecate since 2.2 and doesn't work in 3.1.1
# so now it uses NavigationToolbar2Tk
#
# FigureCanvasTk doesn't display plot but it still shows coordinates in navigation bar
# so it still uses FigureCanvasTkAgg
#
import matplotlib
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg,
#FigureCanvasTk, # doesn't display plot but it still shows coordinates in navigation bar
#NavigationToolbar2TkAgg, deprcated in 2.2 and doesn't work in 3.1.1
NavigationToolbar2Tk
)
from matplotlib.pyplot import Figure
import tkinter as tk
# --- random data ---
import random
import math
y1 = [random.randint(-100, 100) for x in range(60)]
y2 = [math.sin(math.radians(x*6)) for x in range(60)]
# --- GUI ---
root = tk.Tk()
# top frame for canvas and toolbar - which need `pack()` layout manager
top_frame = tk.Frame(root)
top_frame.pack()
# bottom frame for other widgets - which may use other layout manager
bottom_frame = tk.Frame(root)
bottom_frame.pack()
# --- canvas and toolbar in top ---
# create figure
fig = matplotlib.pyplot.Figure()
# create matplotlib canvas using `fig` and assign to widget `top_frame`
canvas = FigureCanvasTkAgg(fig, top_frame)
# get canvas as tkinter widget and put in widget `top`
canvas.get_tk_widget().pack()
# create toolbar in canvas
toolbar = NavigationToolbar2Tk(canvas, top_frame)
#toolbar.update() # I'm not sure if it is needed. Code works without it too
# --- first plot ---
# create first place for plot
ax1 = fig.add_subplot(211)
# draw on this plot
ax1.plot(y1)
# --- second plot ---
# create second place for plot
ax2 = fig.add_subplot(212)
# draw on this plot
ax2.plot(y2)
# --- other widgets in bottom ---
b = tk.Button(bottom_frame, text='Exit', command=root.destroy)
b.pack()
# --- start ----
root.mainloop()