【问题标题】:How to create a delay between mutiple animations on the same graph (matplotlib, python)如何在同一图表上的多个动画之间创建延迟(matplotlib,python)
【发布时间】:2020-06-17 21:41:44
【问题描述】:

这是上一个问题的参考

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

我想绘制这样的图表,它首先动画绿线,然后是橙色线。

目前它会将两条线动画在一起。

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

【问题讨论】:

    标签: python matplotlib animation graph line-plot


    【解决方案1】:

    你可以把步数加倍,先画第一条曲线,然后再画另一条。

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

    【讨论】:

    • 谢谢。我又发布了一个问题。你能帮忙吗?坦杰斯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多