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

戏子无情 提交于 2021-02-11 13:33:17

问题


This is a reference from a previous question

two lines matplotib animation

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

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)))

fig, ax = plt.subplots()
line1, = ax.plot(x, y, color = "r")
line2, = ax.plot(x, z, color = "g")

def update(num, x, y, z, line1, line2):
    line1.set_data(x[:num], y[:num])
    line2.set_data(x[:num], z[:num])
    return [line1,line2]

ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, z, line1, line2],
              interval=295, blit=True)

ax.set_xlabel('Age (day)')
ax.set_ylabel('EO (%)')

plt.show()

I want to plot the graph such that, it first animates the green line, then the orange line.

Currently it animates both the line together.

https://i.stack.imgur.com/ZDlXu.gif


回答1:


You could make the number of steps twice as long, first draw the first curve and then the other one.

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

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)))

fig, ax = plt.subplots()
line1, = ax.plot(x, y, color="r")
line2, = ax.plot(x, z, color="g")

def update(num, x, y, z, line1, line2):
    if num < len(x):
        line1.set_data(x[:num], y[:num])
        line2.set_data([], [])
    else:
        line2.set_data(x[:num - len(x)], z[:num - len(x)])
    return [line1, line2]

ani = animation.FuncAnimation(fig, update, 2 * len(x), fargs=[x, y, z, line1, line2],
                              interval=295, blit=True)

ax.set_xlabel('Age (day)')
ax.set_ylabel('EO (%)')
plt.show()


来源:https://stackoverflow.com/questions/62438829/how-to-create-a-delay-between-mutiple-animations-on-the-same-graph-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!