Python Forum
Tkinter Matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter Matplotlib
#1
I just created a tkinter app that diplays numbers separated with semicolons as graphs. The numbers for x and y are inserted in two textboxes and then plotted. The plots can also be refreshed. I use subplots but when I start the tkinter app, It also opens the subplots without the menu in a separate window. I only want the main window with the buttons and textfields to be displayed.
Here is my code
import matplotlib
matplotlib.use('TkAgg')
import tkinter as tk
from tkinter import * 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,  
NavigationToolbar2Tk) 

class Plotwindow():
    def __init__(self, masterframe, size):
        
        (w,h)=size
        inchsize=(w/25.4, h/25.4)
        #self.figure = Figure(inchsize)
        #self.ax1 = self.figure.add_subplot(111)
        #self.ax2 = self.figure.add_subplot(221)
        
        self.figure, (self.ax1, self.ax2) = plt.subplots(1, 2)
        
        # create canvas as matplotlib drawing area
        self.canvas = FigureCanvasTkAgg(self.figure, master=masterframe)
        self.canvas.get_tk_widget().pack()
        toolbar = NavigationToolbar2Tk(self.canvas, masterframe) 
        toolbar.update() 
  
        # placing the toolbar on the Tkinter window 
        self.canvas.get_tk_widget().pack() 

    
    def plotxy(self, x, y):
        self.ax1.plot(x,y)
        self.ax2.scatter(x,y)
        #self.axes.plot(x,y)
        self.canvas.draw()
    
    def clearplot(self):
        self.ax1.cla()
        self.ax2.cla()
        self.canvas.draw()
        
# class Generatetestdata():
#     def __init__(self):
#         self.index=0 # index of function call
#         self.xmin=0.0
#         4
#         self.xmax=10.0
#         self.nbvalues=500
   
#     def getxy(self):
#         n=self.index
#         x=np.linspace(self.xmin, self.xmax, self.nbvalues)
#         y=np.sin(4*x)*np.exp(-n*x/10)
#         self.index+=1
#         #self.xmax+=5.0
#         return x,y
    
    
class getnumbersfromtextbox():
    def getTextInput(self):
        result=textExample.get("1.0","end")
        return(list(map(float,result.split(";"))))

    def getTextInput2(self):
        result=textExample2.get("1.0","end")
        return(list(map(float,result.split(";"))))
        
    
def plotdata():
    x=dattext.getTextInput()
    y=dattext.getTextInput2()
    # x,y=datgen.getxy()
    pw.plotxy(x,y)


def clear():
    pw.clearplot()

if __name__ == "__main__":
    #datgen = Generatetestdata()
    dattext = getnumbersfromtextbox()
    root = tk.Tk()
    mf= tk.Frame()
    pw=Plotwindow(mf,(200,150))
    mf.pack()
    bf=tk.Frame()
    b1=tk.Button(bf,text="Plot", command=plotdata)
    b1.pack(side=tk.LEFT)
    b2=tk.Button(bf,text="Clear", command=clear)
    b2.pack(side=tk.LEFT)
    bf.pack()
    
    textExample=tk.Text(mf, height=2)
    textExample.pack()

    textExample2=tk.Text(mf, height=2)
    textExample2.pack()
    root.mainloop()
Reply
#2
Shouldn't mf and bf be children of root?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 341 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] Tkinter Matplotlib Animation Graph not rendering dimidgen 3 335 Mar-12-2024, 02:09 PM
Last Post: deanhystad
  How to use rangesliders feature from matplotlib in tkinter? pymn 2 2,916 Feb-28-2022, 05:06 PM
Last Post: pymn
  Tkinter & matplotlib PillyChepper 9 5,524 Nov-23-2019, 10:36 AM
Last Post: PillyChepper
  Making a Matplotlib in Tkinter using a slider Jemeronimo 1 5,616 Dec-05-2018, 08:06 AM
Last Post: Gribouillis
  how to install numpy and matplotlib to Tkinter elwolv 4 4,722 Dec-02-2017, 06:17 PM
Last Post: Barrowman

Forum Jump:

User Panel Messages

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