python - 在 while 循环中定义多行的 Matplotlib 动画
问题描述
我正在尝试绘制 x 和 y 坐标分别由 sk_x(t, tau) 和 sk_y(t, tau) 定义的“n”条线。如果我在循环之外用相同的函数绘制它们,它会很好地绘制函数,但我不能让它们在 animate(i) 函数中工作。一个空的图出现了几分之一秒,然后消失了。问题肯定是我如何处理 animate 函数,但我不能完全弄清楚出了什么问题,它与 tau 作为参数而不是 t 一起工作,但不是相反(参见代码的第二位)。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
v = 1
k = 1
b = 1
c = 100
f = 200
def sk_y(t, tau):
y = v*(t-tau)
return y
def sk_x(t, tau):
x = v*(np.log((1+k*t)/(1+k*tau)))/k
return x
fig = plt.figure()
ax = plt.axes(xlim=(0, sk_x(c,0)), ylim=(0, sk_y(c,0)))
lines = [plt.plot([], [])[0] for _ in range(200)]
def init():
m = 0
while m < c:
lines[m].set_data([], [])
m+=1
return lines
def animate(i):
n=-1
while n<c:
n+=1
t = np.linspace(0,i/2-n,100)
x = sk_x(t+n, n)
y = sk_y(t+n, n)
lines[n].set_data(x, y)
return lines #edited in
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=f, interval=20, blit=True)
plt.show()
将此位用于动画功能可以正常工作
def animate(i):
tau = np.linspace(i*c/f, 0,100)
x = sk_x(c, tau)
y = sk_y(c, tau)
lines[0].set_data(x, y)
return lines
任何帮助将不胜感激!
编辑:在动画函数中添加了返回函数,但它仍然不能像我想要的那样同时对所有行起作用。
解决方案
推荐阅读
- ios - 用动画水平移动 UIImage
- php - Laravel:如何使用查询生成器获取列中的对象
- android - 计时器减少时间以分钟和秒显示在文本视图中
- python - 如何将变量分配给for循环中的if语句?
- c# - 从位图流 C# 中计算 MD5 的问题
- ios - 如何在 SwiftUI 的另一个视图/结构中调用 func?
- apiconnect-test-monitor - 指标视图中获取和延迟之间的差异
- vue.js - vuejs中的axios发布文件返回null
- stanford-nlp - 拆分csv文件的问题
- git - 无法访问 macos 上的 bitbucket 存储库