两行matplotib动画 [英] two lines matplotib animation

查看:96
本文介绍了两行matplotib动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一条动画图表,其中有两条线在同一图中绘制了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()

解决方案

您只需在初始化阶段创建两个艺术家,在更新函数中更新他们的坐标,并且不要忘记返回更新艺术家的列表.

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

解决方案

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

这篇关于两行matplotib动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆