4

I'd like to run a FuncAnimation concurrently to my main program, unfortunately, it's blocking the execution of MainProgram whatever I do.

I've tried:

  1. plt.show(block=False) program continues but plot shows blank
  2. Returning the animation and storing in a variable - MainProgram is blocked
  3. Both: MainProgram runs but plot window is blank

I am aware of this question, however, I assume that given the use of Animation the solutions presented aren't appropriate.

Plotting in a non-blocking way with Matplotlib

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import time

def runGraph():
    # Parameters
    x_len = 200         # Number of points to display
    y_range = [10, 40]  # Range of possible Y values to display

    # Create figure for plotting
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    xs = list(range(0, 200))
    ys = [0] * x_len
    ax.set_ylim(y_range)

    # Create a blank line. We will update the line in animate
    line, = ax.plot(xs, ys)

    # Add labels
    plt.title('TMP102 Temperature over Time')
    plt.xlabel('Samples')
    plt.ylabel('Temperature (deg C)')

    # This function is called periodically from FuncAnimation
    def animate(i, ys):

        # Read temperature (Celsius) from TMP102
        temp_c = np.random.random(1)*40

        # Add y to list
        ys.append(temp_c)

        # Limit y list to set number of items
        ys = ys[-x_len:]

        # Update line with new Y values
        line.set_ydata(ys)

        return line,

    # Set up plot to call animate() function periodically
    ani = animation.FuncAnimation(fig,
        animate,
        fargs=(ys,),
        interval=50,
        blit=True)
    plt.show()

def MainProgram():
     while 1:
         print('Main program')
         time.sleep(0.5)

if __name__ == '__main__':
    runGraph()
    MainProgram()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Charles
  • 238
  • 3
  • 13
  • try first using `plt.ion()` at the beginning of your code, then replace your `plt.show()` for `plt.pause(0.01)` –  Aug 21 '18 at 12:59
  • The plot just shows as a black screen. – Charles Aug 21 '18 at 13:01
  • Possible duplicate of [Plotting in a non-blocking way with Matplotlib](https://stackoverflow.com/questions/28269157/plotting-in-a-non-blocking-way-with-matplotlib) –  Aug 21 '18 at 13:02

1 Answers1

5

The most generic way to solve this is by using the multiprocessing module.

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from multiprocessing import Process
import numpy as np
import time

def runGraph():
    # Parameters
    print('show')
    x_len = 200         # Number of points to display
    y_range = [10, 40]  # Range of possible Y values to display

    # Create figure for plotting
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    xs = list(range(0, 200))
    ys = [0] * x_len
    ax.set_ylim(y_range)

    # Create a blank line. We will update the line in animate
    line, = ax.plot(xs, ys)

    # Add labels
    plt.title('TMP102 Temperature over Time')
    plt.xlabel('Samples')
    plt.ylabel('Temperature (deg C)')

    # This function is called periodically from FuncAnimation
    def animate(i, ys):

        # Read temperature (Celsius) from TMP102
        temp_c = np.random.random(1)*40

        # Add y to list
        ys.append(temp_c)

        # Limit y list to set number of items
        ys = ys[-x_len:]

        # Update line with new Y values
        line.set_ydata(ys)

        return line,


    # Set up plot to call animate() function periodically

    ani = animation.FuncAnimation(fig,
        animate,
        fargs=(ys,),
        interval=50,
        blit=True)
    plt.show()



def MainProgram():
     while 1:
         print('Main program')
         time.sleep(0.5)

if __name__ == '__main__':
    p = Process(target=runGraph)
    p.start()
    MainProgram()
    p.join()