5

I have a piece of code that uses the FuncAnimation method in Python MatPlotLib to generate 50 random Exponential Decay Curves and updating the plot showing each one other the curves as they re generated.Each curves shows up with different colors. I would like to be able to gray out the previos curves as the new one is generated in a set color, say Blue. I hope someone can help.

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

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)       
def main(i):
    # Actual parameters
    A0 = 10 
    K0 = random.uniform(-15,-1)
    C0 = random.uniform(0,10)      

    # Generate some data based on these
    tmin, tmax = 0, 0.5
    num = 20
    t = np.linspace(tmin, tmax, num)
    y = model_func(t, A0, K0, C0)
    ax1.plot(t,y)
def model_func(t, A, K, C):   
        return A * np.exp(K * t)

ani = animation.FuncAnimation(fig, main, interval=1000)

plt.show()

1 Answer 1

6

you have to store the line instance which plot returns and call set_color(color) before you draw again:

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

# an empty variable, whre we store the returned line of plot:
line = None

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)       
def main(i):
    
    # we have to make line global:
    global line

    # Actual parameters
    A0 = 10 
    K0 = random.uniform(-15,-1)
    C0 = random.uniform(0,10)      

    # Generate some data based on these
    tmin, tmax = 0, 0.5
    num = 20
    t = np.linspace(tmin, tmax, num)
    y = model_func(t, A0, K0, C0)
    # check if line already exists, if yes make it gray:
    if line is not None:
        line.set_color('gray')
    # plot returns a list with line instances, one for each line you draw,
    # the comma is used to unpack the one element list
    line, = ax1.plot(t,y, color='red') 

def model_func(t, A, K, C):   
        return A * np.exp(K * t)

# ani = animation.FuncAnimation(fig, main, interval=1000) ## will throw an USER WARMING
# ani = animation.FuncAnimation(fig, main, interval=1000, save_count=100) # save_count=MAXFRAMES  --> OK 

ani = animation.FuncAnimation(fig, main, interval=1000, cache_frame_data=False)  # --> OK
plt.show()

plot result

Sign up to request clarification or add additional context in comments.

2 Comments

Be careful of changing limits in your animation function. Also, the function you pass to the FuncAnimation call back should return a list of artists (all of the lines added) to play nice with blitting.
UserWarning: frames=None which we can infer the length of, did not pass an explicit save_count and passed cache_frame_data=True. To avoid a possibly unbounded cache, frame data caching has been disabled. To suppress this warning either pass cache_frame_data=False or save_count=MAX_FRAMES. ani = animation.FuncAnimation(fig, main, interval=1000)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.