python matplotlib: plotting in another process

后端 未结 3 791
滥情空心
滥情空心 2020-12-04 02:33

EDIT: The ultimate requirement for such a Python program is: Receive data from UART from a external circuitry (which probably is equipped with some sensors), the program wil

3条回答
  •  一生所求
    2020-12-04 03:24

    Made samll modifications to above code. I used Process, Queue, Value, Array in multiprocessing package to reduce the code complexity. Script will plot [0,0], [1,1], [2,2] etc on a graph

    from multiprocessing import Process, Queue, Value, Array
    import time
    import matplotlib.pyplot as plt
    from queue import Empty
    
    def f(q, num, arr1, arr2):
        plt.ion()
        fig = plt.figure()
        fig = plt.figure(1)
        ax = fig.add_subplot(111)
        ax.margins(0.05, 0.05)
    
        #ax.set_autoscale_on(True)
        ax.autoscale(enable=True, axis='both')
        #ax.autoscale(enable=True, axis='y')
        ax.set_ylim(0, 100)
        line1, line2 = ax.plot([], [], 'b-', [], [], 'r-')
    
    
        while True :
            try:
                #val = q.get_nowait()
                val = q.get(timeout = 0.8) # reducing the timeout value will improve the response time on graph whie mouse is moved on it
                #val = q.get()
                if val == 'Exit':
                    break
                if val == 'Go': 
                    x = num.value
                    #print("num.value = ", x)
                    v1 = arr1[0:num.value]
                    v2 = arr2[0:num.value]
                    #print("v1 :", v1)
                    #print("v2 :", v2)
                    line1.set_xdata(range(1, len(v1) + 1, 1))
                    line1.set_ydata(v1)
                    line2.set_xdata(line1.get_xdata())
                    line2.set_ydata(v2)
    
                    ax.relim()
                    ax.autoscale_view(tight=True, scalex=True, scaley=False)
                    fig.canvas.draw()
                    #print ("thread: DRAW")
                    plt.pause(0.05)
            except Empty as e:
                x = num.value
                line1.set_xdata(range(1, len(v1) + 1, 1))
                line1.set_ydata(v1)
                line2.set_xdata(line1.get_xdata())
                line2.set_ydata(v2)
    
                ax.relim()
                ax.autoscale_view(tight=True, scalex=True, scaley=False)
                fig.canvas.draw()
                plt.pause(0.05)
                continue
    
    
    
    if __name__ == '__main__':
        q = Queue()
        num = Value('i', 0)
        arr1 = Array('d', range(100))
        arr2 = Array('d', range(100))
        p = Process(target=f, args=(q,num, arr1, arr2, ))
        p.start()
    
        for i in range(10):
            arr1[i] = i
            arr2[i] = i 
            num.value = i+1
            q.put("Go")   # prints "[42, None, 'hello']"
            time.sleep(1)
        q.put("Exit")
        p.join()
    

提交回复
热议问题