python - 动画不显示使用 matplotlib 传递的参数
问题描述
我采用了一个代码,它绘制了许多粒子的场线,现在我想在一个点移动时为它设置动画,它会改变一个箭袋。此脚本显示一张空白图片。
我曾尝试使用 matplotlib 动画,但我认为问题在于如何传递参数。这是我当前的代码
import numpy as np
import matplotlib.pyplot as plt
import math as ma
from matplotlib import animation
np.seterr(divide='ignore', invalid='ignore')
fig, ax = plt.subplots(1,1)
def seno(a,p,x):
return (int(a*ma.sin(p*x)+7))
# grid size
N = 15
M = 25
ax.set_xlim(0, M)
ax.set_ylim(0, N)
# coordinates
X = np.arange(0, M, 1)
Y = np.arange(0, N, 1)
X, Y = np.meshgrid(X, Y)
# strength
Ex = np.zeros((N, M))
Ey = np.zeros((N, M))
# amount of charges
# computing
qq = [[], []] # to store charges coordinates
def arrows(q,qx,qy,Ex,Ey,qq):
qq[0].append(qy)
qq[1].append(qx)
for i in range(N):
for j in range(M):
denom = ((i - qx) ** 2 + (j - qy) ** 2) ** 1.5
if denom != 0:
Ex[i, j] += q * (j - qy) / denom
Ey[i, j] += q * (i - qx) / denom
E = (Ex ** 2 + Ey ** 2) ** .5
Ex = Ex / E
Ey = Ey / E
def mover(qx,qy,q,Ex,Ey):
for i in range(N):
for j in range(M):
denom = ((i - q[1][1]) ** 2 + (j - q[0][1]) ** 2) ** 1.5
if denom != 0:
Ex[i, j] -= (j - q[0][1]) / denom
Ey[i, j] -= (i - q[1][1]) / denom
q[0].pop()
q[1].pop()
arrows(1,qx,qy,Ex,Ey,q)
E = (Ex ** 2 + Ey ** 2) ** .5
Ex = Ex / E
Ey = Ey / E
arrows(-1,7,25,Ex,Ey,qq)
arrows(1,7,3,Ex,Ey,qq)
C = np.hypot(Ex, Ey)
C = np.hypot(Ex, Ey)
# normalized values for arrows to be of equal length
def animate(i,args):
mover(7,seno(args[0],args[1],i))
ax.plot(*qq, 'bo')
ax.quiver(X, Y, Ex, Ey, C, pivot='mid')
anim = animation.FuncAnimation(fig, animate, (4,1), frames=200, interval=20, blit=True)
plt.show()
我希望动画一个点,而它移动它会改变一个颤动。
解决方案
推荐阅读
- pine-script - 如何引用一个酒吧并永远记住那个酒吧?
- javascript - Css 样式未在我尝试使用 javascript 打印的页面中应用
- sql-server - 使用 DACPAC,当表上有触发器时,如何重命名列?
- c++ - 如何一次从文件中读取几个变量?(c++)
- sql - HAVING 子句中多个聚合函数的性能影响
- kubernetes - Kubernetes 中未指定资源时,默认分配是什么?
- vba - Powerpoint VBA - 从形状中删除边框/线条/轮廓
- java - 如何在 X-ARR-ClientCert 的邮递员标头中传递 X.509 证书字符串
- here-api - HereAPI - 无法到达请求的航点
- python - jupyter lab的全新conda安装无法连接到内核