Bottom Page

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:

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()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Python code unable to show Bokeh line chart kirito85 2 141 Feb-06-2019, 07:52 AM
Last Post: kirito85
  Tips on surface plot HW question Cwcox 1 265 Nov-14-2018, 11:45 PM
Last Post: Larz60+
  How to customize x axis in matplotlib.pyplot for a scatter plot? wlsa 9 505 Nov-10-2018, 01:32 AM
Last Post: wlsa
  Adding text to plot Pythcoronas 1 328 Sep-13-2018, 12:27 PM
Last Post: baby_quant
  plot the mean in a bar diagram sussii 3 464 May-23-2018, 11:27 PM
Last Post: wavic
  How to plot date series in matplotlib? StrybolData 2 1,928 Jan-25-2018, 07:13 PM
Last Post: StrybolData
  Python UI - Getting Label to show to left of QLineEdit ijosefson 1 696 Nov-11-2017, 01:54 AM
Last Post: ijosefson
  Removing data in a plot ulrich48155 3 1,337 Jun-19-2017, 06:31 PM
Last Post: zivoni
  How to plot a Graph Kamo 2 1,400 May-12-2017, 12:22 PM
Last Post: zivoni
  matplotlib : Raster Plot adithyakrish 0 1,107 Apr-24-2017, 10:46 AM
Last Post: adithyakrish

Forum Jump:


Users browsing this thread: 1 Guest(s)