Search code examples
pythonmatplotlibanimationgraphline-plot

Plot n animated growing line plots on the same graph (matplotlib, python)


I am trying to plot multiple animated line plots on the same graph with a delay of 1 second between each line plot in matplotlib python.

My previous question: How to create a delay between mutiple animations on the same graph (matplotlib, python)

I have a list of lists: [[10,20,30],[40,50,60],[2,3,5],[200,300,500]]

I want to plot each element of the list of list as animated line plot on the same graph. Animation should plot the 1st list , and then plot the second list, then the third list and then the fourth list. They shouldn't be plotted simultaneously.

Thanks

Plot will look like:


Solution

  • You can make use of matplotlibs interactive mode as shown here:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    
    fig, ax = plt.subplots()
    
    plt.ion()   # set interactive mode
    plt.show()
    x = np.arange(130, 190, 1)
    y = 97.928 * np.exp(- np.exp(-  0.1416 * (x - 146.1)))
    z = 96.9684 * np.exp(- np.exp(-0.1530 * (x - 144.4)))
    y_z=[y,z]
    ax.set_xlim(x[0],x[-1])
    ax.set_ylim(min(y[0],z[0]),max(y[-1],z[-1]))
    
    color=['green','red']
    
    k=0
    for i in y_z:
        for j in range(len(i)):
            line, = ax.plot(x[:j],i[:j],color=color[k])
            plt.gcf().canvas.draw()
            plt.pause(0.1)
        k=k+1
    

    enter image description here