Home > database >  how to plot live graph in python with tkinter
how to plot live graph in python with tkinter

Time:07-21

this code is for taking readings from arduino and make a graph after taking all the readings. I want live plotting.

def takeRead():
    ser = serial.Serial()
    ser.baudrate = 115200
    ser.port = "COM7" # "COM#" this must match with connected port
    ser.open() #opening serial port
    while True:
        global data
        data = []
        x = []
        for j in range(8000):
            line = ser.readline() # read a byte string
            if line:
                string = line.decode() # convert the byte string to a unicode string
                res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
                data.append(res) #append readings to store 
                print(res)
                x0= j*0.9
                x.append(x0)
                print(x0) 
            #clear axes to clear the previous plot
        #showing legend    ax.clear()
        data = [val for sublist in data for val in sublist]
        ax.plot(x,data,color='b',label='actual')
        canvas.draw()
        ax.legend()
        break
    print(data)
    ser.close()

I tried many things like, putted

ax.plot()
canvas.draw()

in my loop but it was lagging. here is code

def takeRead():
    ser = serial.Serial()
    ser.baudrate = 115200
    ser.port = "COM7" # "COM#" this must match with connected port
    ser.open() #opening serial port
    while True:
        global data,x
        data = []
        x = []
        for j in range(8000):
            line = ser.readline() # read a byte string
            if line:
                string = line.decode() # convert the byte string to a unicode string
                res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
                data.append(res) #append readings to store 
                print(res)
                x0= j*0.9
                x.append(x0)
                print(x0)
            ax.plot(x,data,color='b',label='actual')
            canvas.draw()
            #clear axes to clear the previous plot
        #showing legend    ax.clear()
        
        ax.legend()
        
        break
    ser.close()

also tried 'FuncAnimation' from 'matplotlib.animation' in this case, the graph was still lagging, but taking about 4000 readings it suddenly jump to zero and then skip readings (may be due to flushing of serial buffer or something)

here is the code for FunctAnimation

def animate(i):
    #print(z)
    if(z==1):
        #data_string=arduino.readline()
        #print(data_string)
        data=int(arduino.readline()) ## remove int() if you want string data to be represented
        ys.append(data)
        print(data)
        ax.clear()
        ax.plot([i for i in range(len(ys))],ys)  ## plots the graph
        ax.set(xlabel='Time in x100 ms',ylabel='Magnitude')
        ax.set_title('POT Readings')
    else:
        print("Not connected to com port")
anim = animation.FuncAnimation(fig, animate, interval=10)

plt.show()

one person suggested me to update root window only after some interval but my bad, i don't know how to do that

CodePudding user response:

This code is some what solution to my question, still it updates for random, sometime it update 3 time , sometime even more than 10 times, but not always updating It should update graph after 1000 ms = 1 sec , but after sometime it stops updating

def takeRead():
    ser = serial.Serial()
    ser.baudrate = 115200
    ser.port = "COM7" # "COM#" this must match with connected port
    ser.open() #opening serial port
    while True:
        global data,x
        data = []
        x = []
        for j in range(8000):
            line = ser.readline() # read a byte string
            if line:
                string = line.decode() # convert the byte string to a unicode string
                res = [int(i) for i in string.split() if i.isdigit()] #saperating integer from string
                data.append(res) #append readings to store 
                print(res)
                x0= j*0.9
                x.append(x0)
                print(x0) 
            #clear axes to clear the previous plot
        #showing legend    ax.clear()
        data = [val for sublist in data for val in sublist]
        break
    #threading.Thread.join()
    #print(data)
    ser.close()

def plotdata():
    ax.plot(x,data,color='b',label='actual')
    canvas.draw()
    root.after(1000,plotdata)
  • Related