pythonmatplotlibscatter-plot

python matplotlib update scatter plot from a function


I am trying to automatically update a scatter plot. The source of my X and Y values is external, and the data is pushed automatically into my code in a non-predicted time intervals (rounds).

I have only managed to plot all the data when the whole process ended, whereas I am trying to constantly add and plot data into my canvas.

What I DO get (at the end of the whole run) is this: enter image description here

Whereas, what I am after is this: enter image description here

A simplified version of my code:

import matplotlib.pyplot as plt

def read_data():
    #This function gets the values of xAxis and yAxis
    xAxis = [some values]  #these valuers change in each run
    yAxis = [other values] #these valuers change in each run

    plt.scatter(xAxis,yAxis, label  = 'myPlot', color = 'k', s=50)     
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()

Solution

  • There are several ways to animate a matplotlib plot. In the following let's look at two minimal examples using a scatter plot.

    (a) use interactive mode plt.ion()

    For an animation to take place we need an event loop. One way of getting the event loop is to use plt.ion() ("interactive on"). One then needs to first draw the figure and can then update the plot in a loop. Inside the loop, we need to draw the canvas and introduce a little pause for the window to process other events (like the mouse interactions etc.). Without this pause the window would freeze. Finally we call plt.waitforbuttonpress() to let the window stay open even after the animation has finished.

    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.ion()
    fig, ax = plt.subplots()
    x, y = [],[]
    sc = ax.scatter(x,y)
    plt.xlim(0,10)
    plt.ylim(0,10)
    
    plt.draw()
    for i in range(1000):
        x.append(np.random.rand(1)*10)
        y.append(np.random.rand(1)*10)
        sc.set_offsets(np.c_[x,y])
        fig.canvas.draw_idle()
        plt.pause(0.1)
    
    plt.waitforbuttonpress()
    

    (b) using FuncAnimation

    Much of the above can be automated using matplotlib.animation.FuncAnimation. The FuncAnimation will take care of the loop and the redrawing and will constantly call a function (in this case animate()) after a given time interval. The animation will only start once plt.show() is called, thereby automatically running in the plot window's event loop.

    import matplotlib.pyplot as plt
    import matplotlib.animation
    import numpy as np
    
    fig, ax = plt.subplots()
    x, y = [],[]
    sc = ax.scatter(x,y)
    plt.xlim(0,10)
    plt.ylim(0,10)
    
    def animate(i):
        x.append(np.random.rand(1)*10)
        y.append(np.random.rand(1)*10)
        sc.set_offsets(np.c_[x,y])
    
    ani = matplotlib.animation.FuncAnimation(fig, animate, 
                    frames=2, interval=100, repeat=True) 
    plt.show()