Embedding Matplotlib live plot data from Arduino in tkinter canvas

前端 未结 1 969
-上瘾入骨i
-上瘾入骨i 2020-12-11 09:01

I\'ve only been using Python for a couple of weeks. I have no problems plotting the data coming in from the Arduino with Matplotlib. However the plot shows up as a pop-windo

相关标签:
1条回答
  • 2020-12-11 09:20

    The main loop of tk will take care of the animation, you therefore shouldn't use plt.ion() or plt.pause().

    The animating function will be called every interval seconds. You cannot use a while True loop inside this function.

    There is no reason whatsoever to supply the animating function to the FigureCanvasTkAgg.

    Don't use blit=True unless you know what you're doing. With an interval of one second this is anyways not necessary.

    Update the line instead of replotting it in every iteration step.

    #import serial
    from Tkinter import *
    from matplotlib import pyplot as plt
    import matplotlib.animation as animation
    from matplotlib import style
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    
    
    root = Tk()
    root.geometry('1200x700+200+100')
    root.title('This is my root window')
    root.state('zoomed')
    root.config(background='#fafafa')
    
    xar = []
    yar = []
    
    style.use('ggplot')
    fig = plt.figure(figsize=(14, 4.5), dpi=100)
    ax1 = fig.add_subplot(1, 1, 1)
    ax1.set_ylim(0, 100)
    line, = ax1.plot(xar, yar, 'r', marker='o')
    #ser = serial.Serial('com3', 9600)
    
    def animate(i):
        #ser.reset_input_buffer()
        #data = ser.readline().decode("utf-8")
        #data_array = data.split(',')
        #yvalue = float(data_array[1])
        yar.append(99-i)
        xar.append(i)
        line.set_data(xar, yar)
        ax1.set_xlim(0, i+1)
    
    
    plotcanvas = FigureCanvasTkAgg(fig, root)
    plotcanvas.get_tk_widget().grid(column=1, row=1)
    ani = animation.FuncAnimation(fig, animate, interval=1000, blit=False)
    
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题