Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How do I make my plot show up?
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()


    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.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.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.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)
    if ABradiobutton == 1:
        t,x = solveMBK()

    elif Radio_Var == 2:
        t,x = solveMBK()

    tk.Label(Graph_Frame, text = "Mass-Spring-Damper Plot").pack()

    canvas = FigureCanvasTkAgg(Fig, Graph_Frame)



    toolbar = NavigationToolbar2Tk(canvas, Graph_Frame)



def CloseWindow():




def funcPlot(input_list, mscale, bscale, kscale, a, canvas, event=None):

    data = solveMBK(input_list)

    a.plot(data[0], data[1])



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_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_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_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_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)


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_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_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_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_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_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_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_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_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)


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  I have homework that I've made no effort to show any code that I'm stuck on Ronaldinho 1 211 May-30-2019, 07:18 PM
Last Post: nilamo
  matplotlib : Raster Plot adithyakrish 1 1,353 May-28-2019, 05:04 AM
Last Post: heiner55
  How to create a new plot in a figure right below the first one after click event? codexx 1 154 May-26-2019, 12:52 PM
Last Post: heiner55
  Plot a function dxfrelince 3 184 May-21-2019, 12:23 PM
Last Post: DeaD_EyE
  Python code unable to show Bokeh line chart kirito85 2 267 Feb-06-2019, 07:52 AM
Last Post: kirito85
  Tips on surface plot HW question Cwcox 1 414 Nov-14-2018, 11:45 PM
Last Post: Larz60+
  How to customize x axis in matplotlib.pyplot for a scatter plot? wlsa 9 1,042 Nov-10-2018, 01:32 AM
Last Post: wlsa
  Adding text to plot Pythcoronas 1 485 Sep-13-2018, 12:27 PM
Last Post: baby_quant
  plot the mean in a bar diagram sussii 3 624 May-23-2018, 11:27 PM
Last Post: wavic
  How to plot date series in matplotlib? StrybolData 2 2,707 Jan-25-2018, 07:13 PM
Last Post: StrybolData

Forum Jump:

Users browsing this thread: 1 Guest(s)