【问题标题】:two lines matplotib animation [duplicate]两行matplotlib动画[重复]
【发布时间】:2018-03-08 04:05:23
【问题描述】:

我正在尝试制作一个动画图表,其中两条线在同一图中绘制 x、y 和 x、z 动画,但我没有成功。你能帮帮我吗?

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()
line, = ax.plot(x, y, color = "r")

def update(num, x, y, line):
     line.set_data(x[:num], y[:num])
     line.axes.axis([130, 200, 0, 110])
     return line,


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


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


  plt.show()

【问题讨论】:

    标签: python matplotlib figure


    【解决方案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):
        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()
    

    【讨论】:

    • 我认为这是一个非常简单和有用的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多