python matplotlib: plotting in another process

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 will process these data, and draw a dynamically updated curve on the computer screen.

So, I want to plot dynamically, the following test script starts a sub-process, and in that process, it accepts data from parent process through a Queue, and plot data accordingly.

But when the script is run, only an empty figure is shown, I can see the console prints "put:" and "got:" messages, meaning both parent and subprocess are running and communicating, but nothing happens in the GUI figure window.

Furthermore, the GUI window is not responsive and if I click on the window, it will crash.

The system is Windows 10, 64 bit. Python version is 2.7 (32bit)

What's the problem here? thank you!

import matplotlib.pyplot as plt
import multiprocessing as mp
import random
import numpy
import time

def worker(q):
    plt.ion()
    ln, = plt.plot([], [])
    plt.show()

    while True:
        obj = q.get()
        n = obj + 0
        print "sub : got:", n

        ln.set_xdata(numpy.append(ln.get_xdata(), n))
        ln.set_ydata(numpy.append(ln.get_ydata(), n))
        plt.draw()

if __name__ == '__main__':
    queue = mp.Queue()
    p = mp.Process(target=worker, args=(queue,))
    p.start()

    while True:
        n = random.random() * 5
        print "main: put:", n
        queue.put(n)
        time.sleep(1.0)

Solution 1:

You have to rescale, otherwise nothing will appear:

This works on my computer :

import matplotlib.pyplot as plt
import multiprocessing as mp
import random
import numpy
import time

def worker(q):
    #plt.ion()
    fig=plt.figure()
    ax=fig.add_subplot(111)
    ln, = ax.plot([], [])
    fig.canvas.draw()   # draw and show it
    plt.show(block=False)

    while True:
        obj = q.get()
        n = obj + 0
        print "sub : got:", n

        ln.set_xdata(numpy.append(ln.get_xdata(), n))
        ln.set_ydata(numpy.append(ln.get_ydata(), n))
        ax.relim()

        ax.autoscale_view(True,True,True)
        fig.canvas.draw()

if __name__ == '__main__':
    queue = mp.Queue()
    p = mp.Process(target=worker, args=(queue,))
    p.start()

    while True:
        n = random.random() * 5
        print "main: put:", n
        queue.put(n)
        time.sleep(1.0)

Solution 2:

Till now I'd like to flag my following sample program as the answer to my question. It definitely is not the perfect one, or maybe it's even not the correct way to do that in Python and matplotlib.

I think the important thing to not cause unresponsiveness on the figure is not to hang the "UI" thread, which when the UI is shown, matplotlib probably is running a event loop on it, so if I put any time.sleep(0.1) or call Queue.get() which block the thread execution, the figure window will just hang.

So instead of blocking the thread at "Queue.get()", I choose to use "Queue.get_nowait()" as a polling method for incoming new data. The UI thread (ie, matplotlib figure window updating worker) will only block at matplotlib.pyplot.pause(), which will not suspend the event loop I believe.

If there is another call in matplotlib that can block and wait for a signal, I think that would be better than this polling approach.

At first I see multiprocessing examples with matplotlib, so I was trying to use multiple processes for concurrency. But it seems that you just need to take care of the synchronization yourself, it is okay to use multithreading instead. And multithreading has the benefit of sharing data within one process. So the following program utilized threading module instead of multiprocessing.

The following is my test program, I can run it on Windows 7 (64 bit) with Python 2.7, and the Figure Window is responsive at this rate of updating, you can drag it, resize it and so on.

#!/usr/bin/python
# vim: set fileencoding=utf-8:

import random
import time
import Queue
import threading
import numpy as np
import matplotlib.pyplot as plt

## Measurement data that are shared among threads
val1 = []
val2 = []
lock = threading.Lock()

def update_data_sync(x, y):
    lock.acquire()
    val1.append(x)
    val2.append(y)
    if len(val1) > 50:
        del val1[0]
    if len(val2) > 50:
        del val2[0]
    lock.release()

def get_data_sync():
    lock.acquire()
    v1 = list(val1)
    v2 = list(val2)
    lock.release()
    return (v1, v2)

def worker(queue):
    plt.ion()
    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, 1)

    line1, line2 = ax.plot([], [], 'b-', [], [], 'r-')

    while True:
        need_draw = False
        is_bye = False

        while True:
            ## Try to exhaust all pending messages
            try:
                msg = queue.get_nowait()
                if msg is None:
                    print "thread: FATAL, unexpected"
                    sys.exit(1)
                if msg == 'BYE':
                    print "thread: got BYE"
                    is_bye = True
                    break
                # Assume default message is just let me draw
                need_draw = True
            except Queue.Empty as e:
                # Not 'GO' or 'BYE'
                break

        ## Flow control
        if is_bye:
            break
        if not need_draw:
            plt.pause(0.33)
            continue;

        ## Draw it
        (v1, v2) = get_data_sync()
        line1.set_xdata(range(1, len(v1) + 1, 1))
        # Make a clone of the list to avoid competition on the same dataset
        line1.set_ydata(v1)
        line2.set_xdata(line1.get_xdata())
        line2.set_ydata(v2)

        ## Adjust view
        #ax.set_xlim(0, len(line1.get_ydata()) + 1)
        #ax.set_ylim(0, 1)
        ## (??) `autoscale' does not work here...
        #ax.autoscale(enable=True, axis='x')
        #ax.autoscale(enable=True, axis='y')
        ax.relim()
        ax.autoscale_view(tight=True, scalex=True, scaley=False)

        ## "Redraw"
        ## (??) Maybe pyplot.pause() can ensure visible redraw
        fig.canvas.draw()
        print "thread: DRAW"
        plt.pause(0.05)
    ## Holy lengthy outermost `while' loop ends here

    print "thread: wait on GUI"
    plt.show(block=True)
    plt.close('all')
    print "thread: worker exit"
    return

def acquire_data():
    # Fake data for testing
    if not hasattr(acquire_data, 'x0'):
        acquire_data.x0 = 0.5
    x = int(random.random() * 100) / 100.0
    while np.abs(x - acquire_data.x0) > 0.5:
        x = int(random.random() * 100) / 100.0
    acquire_data.x0 = x

    y = 0.75 * np.abs(np.cos(i * np.pi / 10)) + 0.15

    return (x, y)

if __name__ == "__main__":
    queue = Queue.Queue()
    thr = threading.Thread(target=worker, args=(queue, ))
    thr.start()

    for i in range(200):
        (x, y) = acquire_data()
        update_data_sync(x, y)
        #print "main: val1: {}. val2: {}".format(x, y)
        queue.put("GO")
        time.sleep(0.1)
    queue.put('BYE')

    print "main: waiting for children"
    thr.join()
    print "main: farewell"