Python Forum
How do I make my plot show up?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make my plot show up?
#1
For this assignment, we are tasked with making a gui program that allows the user to enter many different values such as, x0, dx(0)/dt, t-final, dt, constant load A, sine load (Bt) and parameters for m, b and k. When the plot button is pressed, our plot window should open and we should be able to change the m, b and k sliders and we should see this response affect the graph.

My problem seems to be getting my function plotted. The error is coming within the function where I am trying to use my solveMBK function inside my PlotWindow function, I am not sure how to include the values from my sliders as well as the other values that are inputted in the start up window. Here is a look at my 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import tkinter as tk
 
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
 
from matplotlib.figure import Figure
 
import numpy as np
 
 
def solveMBK(inlist):
 
    x0 = inlist[0]
    dx0 = inlist[1]
    dt = inlist[2]
    m = inlist[3]
    b = inlist[4]
    k = inlist[5]
    tf = inlist[6]
    Z = inlist[7]
 
    t = np.arange(0,tf,dt)
 
    z0 = np.zeros_like(t)
    z1 = np.zeros_like(t)
 
    z0[0] = x0
    z1[0] = dx0
 
    for c in range(len(t)-1):
        z0[c+1] = z0[c] + z1[c]*dt
        z1[c+1] = z1[c] + ((ABradiobutton(Z,t[c]) - k*z0[c] - b*z1[c]) / m)
 
    x = z0
 
    return t,x
 
def ABradiobutton(Z, t):
    if Z == 1:
        A = float(A_entry.get())
        return A
 
    elif Z == 2:
        B = float(B_entry.get())
        return np.sin(B*t)
 
def PlotWindow():
 
    root1 = tk.Tk()
 
    root1.title("Plot")
 
    Mmin = float(Mmin_entry.get())
 
    Mmax = float(Mmax_entry.get())
 
    bmin = float(bmin_entry.get())
 
    bmax = float(bmax_entry.get())
 
    kmin = float(kmin_entry.get())
 
    kmax = float(kmax_entry.get())
 
    mscale = tk.Scale(root1, from_=Mmin, to=Mmax, label="m", bd=2, length=200, orient=tk.HORIZONTAL, command = funcPlot)
 
    mscale.set((Mmin+Mmax)/2)
 
    mscale.grid(row=1, column=0)
 
    bscale = tk.Scale(root1, from_=bmin, to=bmax, label="b", bd=2, length=200, orient=tk.HORIZONTAL, command = funcPlot)
 
    bscale.set((bmin+bmax)/2)
 
    bscale.grid(row=3, column=0)
 
    kscale = tk.Scale(root1, from_=kmin, to=kmax, label="k", bd=2, length=200, orient=tk.HORIZONTAL, command = funcPlot)
 
    kscale.set((kmin+kmax)/2)
 
    kscale.grid(row=5, column=0)
 
    tk.Label(root1, text = " ").grid(row=6, column=0)
 
    tk.Button(root1, text="Back", command=root1.destroy).grid(row=7, column=0)
 
    Graph_Frame = tk.Frame(root1)
 
    Graph_Frame.grid(row=2, column=2, columnspan=10, rowspan=10)
 
    Fig = Figure(figsize=(5.5,4))
 
    a = Fig.add_subplot(111)
[error]
    if ABradiobutton == 1:
        t,x = solveMBK()
        a.plot(t,x)
 
    elif Radio_Var == 2:
        t,x = solveMBK()
        a.plot(t,x)
[/error]
 
    tk.Label(Graph_Frame, text = "Mass-Spring-Damper Plot").pack()
 
    canvas = FigureCanvasTkAgg(Fig, Graph_Frame)
 
    canvas.draw()
 
    canvas.get_tk_widget().pack()
 
    toolbar = NavigationToolbar2Tk(canvas, Graph_Frame)
 
    toolbar.update()
 
    canvas.get_tk_widget().pack()
 
 
 
def CloseWindow():
 
    root.quit()
 
    root.destroy()
 
    exit()
 
 
def funcPlot(input_list, mscale, bscale, kscale, a, canvas, event=None):
    input_list[0]=float(x0_Entry.get())
    input_list[1]=float(dxdt_Entry.get())
    input_list[2]=float(dt_entry.get())
    input_list[3]=float(mscale.get())
    input_list[4]=float(bscale.get())
    input_list[5]=float(kscale.get())
    input_list[6]=float(tfinal_entry.get())
    input_list[7]=float(Radio_Var.get())
 
    data = solveMBK(input_list)
 
    a.plot(data[0], data[1])
 
    canvas.draw()
 
    return
 
root = tk.Tk()
 
root.title("Numerical solution of a second order differential equation")
 
tk.Label(root, text = "Differential Equation:").grid(row=0, column=0, sticky=tk.E)
 
tk.Label(root, text = "m d2x/dt2 + b dx/dt + kx = f(x)").grid(row=0, column=1)
 
x0_Start = tk.IntVar()
 
x0_Start.set("0")
 
x0_Entry = tk.Entry(root, width=7, textvariable = x0_Start)
 
tk.Label(root, text = "x(0) = ").grid(row=1, column=0, stick=tk.E), x0_Entry.grid(row=1, column=1, sticky=tk.W)
 
dxdt_Start = tk.IntVar()
 
dxdt_Start.set("0")
 
dxdt_Entry = tk.Entry(root, width=7, textvariable = dxdt_Start)
 
tk.Label(root, text = "dx(0)/dt= ").grid(row=2, column=0, sticky=tk.E), dxdt_Entry.grid(row=2, column=1, sticky=tk.W)
 
A_start = tk.IntVar()
 
A_start.set("1")
 
A_entry = tk.Entry(root, width=7, textvariable = A_start)
 
tk.Label(root, text = "A = ").grid(row=6, column=1, sticky=tk.E), A_entry.grid(row=6, column=2, sticky=tk.W)
 
B_start = tk.IntVar()
 
B_start.set("0")
 
B_entry = tk.Entry(root, width=7, textvariable=B_start)
 
tk.Label(root, text= "B =").grid(row=7,column=1, sticky=tk.E), B_entry.grid(row=7, column=2, sticky=tk.W)
 
tk.Label(root, text = " ").grid(row=5, column=0, sticky=tk.E)
 
Radio_Var = tk.IntVar()
 
tk.Radiobutton(root, text="A", value = 1, variable=Radio_Var).grid(row=6, column=1, sticky = tk.W)
 
tk.Radiobutton(root, text="sin(Bt)", value = 2, variable=Radio_Var).grid(row=7, column=1, sticky = tk.W)
 
Radio_Var.set(1)
 
tk.Label(root, text = "f(x) = ").grid(row=6, column=0, sticky=tk.E)
 
tk.Label(root, text = " ").grid(row=8, column=0, sticky=tk.E)
 
tfinal_start = tk.IntVar()
 
tfinal_start.set("10")
 
tfinal_entry = tk.Entry(root, width = 7, textvariable=tfinal_start)
 
tk.Label(root, text = "tfinal = ").grid(row=9, column=0, sticky=tk.E), tfinal_entry.grid(row=9, column=1, sticky=tk.W)
 
dt_start = tk.IntVar()
 
dt_start.set("0.001")
 
dt_entry = tk.Entry(root, width = 7, textvariable=dt_start)
 
tk.Label(root, text = "dt = ").grid(row=9, column=1, sticky=tk.E), dt_entry.grid(row=9, column=2, sticky=tk.W)
 
tk.Label(root, text = " ").grid(row=10, column=0, sticky=tk.E)
 
Mmin_start = tk.IntVar()
 
Mmin_start.set("1")
 
Mmin_entry = tk.Entry(root, width=7, textvariable=Mmin_start)
 
tk.Label(root, text = "Mmin = ").grid(row=11, column=0, sticky=tk.E), Mmin_entry.grid(row=11, column=1, sticky=tk.W)
 
Mmax_start = tk.IntVar()
 
Mmax_start.set("100")
 
Mmax_entry = tk.Entry(root, width=7, textvariable=Mmax_start)
 
tk.Label(root, text = "Mmax = ").grid(row=11,column=1, sticky=tk.E), Mmax_entry.grid(row=11, column=2, sticky=tk.W)
 
bmin_start = tk.IntVar()
 
bmin_start.set("1")
 
bmin_entry = tk.Entry(root, width=7, textvariable=bmin_start)
 
tk.Label(root, text = "bmin = ").grid(row=12, column=0, sticky=tk.E), bmin_entry.grid(row=12, column=1, sticky=tk.W)
 
bmax_start = tk.IntVar()
 
bmax_start.set("250")
 
bmax_entry = tk.Entry(root, width=7, textvariable=bmax_start)
 
tk.Label(root, text= "bmax = ").grid(row=12, column=1, sticky=tk.E), bmax_entry.grid(row=12,column=2,sticky=tk.W)
 
kmin_start = tk.IntVar()
 
kmin_start.set("1")
 
kmin_entry = tk.Entry(root, width=7, textvariable=kmin_start)
 
tk.Label(root, text= "kmin = ").grid(row=13, column=0, sticky=tk.E), kmin_entry.grid(row=13, column=1, sticky=tk.W)
 
kmax_start = tk.IntVar()
 
kmax_start.set("500")
 
kmax_entry = tk.Entry(root, width=7, textvariable=kmax_start)
 
tk.Label(root, text="kmax = ").grid(row=13, column=1, sticky=tk.E), kmax_entry.grid(row=13, column=2, sticky=tk.W)
 
tk.Button(root, text = "Quit", command=CloseWindow, width=10).grid(row=14, column=0)
 
tk.Button(root, text= "Plot", command=PlotWindow, width=10).grid(row=14, column=3)
 
root.mainloop()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020