two lines matplotib animation [duplicate]

给你一囗甜甜゛ 提交于 2020-08-03 11:08:15

问题


I'm trying to make an animated chart with two lines plotting x, y and x, z with animation in the same figure, but I'm not succeeding. Can you help me?

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

回答1:


You simply have to create two artists at the initialisation stage, update their coordinates in the update function, and not forget to return a list of updated artists.

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://stackoverflow.com/questions/49165233/two-lines-matplotib-animation

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