matplotlib and tkinter to plot in real time

0

I am trying to graph a series of data from a text file, but my idea is that this graph only appears when I press a button on a GUI made in tkinter. However, at the moment of pressing the button the graph appears but without the data, that is, it appears in white.

Well, I'll give you my code and I hope you can help me:

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from tkinter import *
    from tkinter import ttk

    class ini():
        def __init__(self):
           self.fig = plt.figure()
           self.ax1 = self.fig.add_subplot(1,1,1)
           self.datosy=[]
           self.datosx=[]

        def animada(self):
           datostext = open('datos.txt','r').read()
           lines = datostext.split('\n')
           for line in lines:
              if len(line)>1:
                 self.x, self.y = line.split(",")
                 self.datosx.append(self.x)
                 self.datosy.append(self.y)
          self.ax1.clear()
          self.ax1.plot(self.datosx,self.datosy)


        def fungraf(self):
           self.ani = animation.FuncAnimation(self.fig, 
                                          self.animada,   #serias dudas aca  
                                          interval=1000)    
           plt.show() 



    h=ini()
    raiz=Tk()
    raiz.geometry("600x600")

    boton=ttk.Button(raiz,text="start",command=h.fungraf)
    boton.pack()


    raiz.mainloop()  
    
asked by Camilo Martinez 27.03.2018 в 20:24
source

1 answer

0

Ok I was looking for several example codes and I had not fixed that the animated function or rather the function that updates the data of the graph, needs a parameter ("i") that indicates the number of the animation table, correcting the function would look like this:

    def animada(self,i):    #aqui es donde agrego la i
       datostext = open('datos.txt','r').read()
       lines = datostext.split('\n')
       for line in lines:
          if len(line)>1:
             self.x, self.y = line.split(",")
             self.datosx.append(self.x)
             self.datosy.append(self.y)
      self.ax1.clear()
      self.ax1.plot(self.datosx,self.datosy)

ok I understand that the solution was quite simple, however if someone at some point comes to have the same problem, I hope this can be useful.

    
answered by 27.03.2018 в 23:52