python - 带 blitting 的 3d 散点图动画
问题描述
我想制作一个 3d 线图动画,并在最后一点绘制一个点,就像一个粒子的轨迹。在没有 blitting 的情况下,我使用散点图和线 plot3d 来完成,在更新函数中更新这两个图。我尝试以相同的方式使用 blitting 进行操作,但会引发以下错误
TypeError: 'Path3DCollection' object is not iterable
我的代码如下
import matplotlib.pyplot as m
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
def ranstep(start,step,stop):
numberlist = int((stop - start)/step + 1)
lista = [0]*numberlist
i = 0
while i <= numberlist:
if i == 0:
lista[i] = start
i = i + 1
if i != 0 and i < numberlist - 1:
lista[i] = lista[i - 1] + step
i = i + 1
if i != 0 and i == numberlist - 1:
lista[i] = stop
break
return(lista)
k1 = 10.0
k2 = 28.0
k3 = 8.0/3.0
czeroA = 1
czeroB = 0
czeroC = 0
step = 0.001
tfinal = 100
time = list(ranstep(0,step,tfinal))
cA = [0]*len(time)
cB = [0]*len(time)
cC = [0]*len(time)
i = 0
while i <= len(time) - 1:
if i == 0:
cA[i] = czeroA
cB[i] = czeroB
cC[i] = czeroC
i = i + 1
else:
cA[i] = cA[i - 1] + k1*(cB[i - 1] - cA[i - 1])*step
cB[i] = cB[i - 1] + (cA[i - 1]*(k2 - cC[i - 1]) - cB[i-1])*step
cC[i] = cC[i - 1] + (cA[i - 1]*cB[i-1] - k3*cC[i-1])*step
i = i + 1
fig, ax = m.subplots()
ax = fig.add_subplot(111, projection='3d')
line, = ax.plot3D([], [], [], c = 'blue', lw = 1)
line, = ax.scatter([],[],[], c = 'red', s = 30)
def init():
ax.set_xlim([-20,20])
ax.set_ylim([-20,20])
ax.set_zlim([0,50])
ax.set_xlabel('$x(t)$')
ax.set_ylabel('$y(t)$')
ax.set_zlabel('$z(t)$')
return line,
def update(frame):
line.set_data(cA[0:frame*50],cB[0:frame*50])
line.set_3d_properties(cC[0:frame*50])
line.set_data(cA[frame*500,frame*500 + 1],cB[frame*500,frame*500 + 1])
line.set_3d_properties(cC[frame*500,frame*500 + 1])
return line,
ani = animation.FuncAnimation(fig, update,interval=5, frames = len(time),init_func=init, blit=True)
m.show()
解决方案
推荐阅读
- java - Java中的OpenPGP公钥导入
- c++ - CStdioFile 不能处理大于 2GB 的文件?
- git - 在 Github Actions 中为 [not] 来自 fork 的 PR 获取目标分支 ref
- symfony - Symfony 基于角色的路由重定向
- python - python regex 模块可以匹配非 utf 编码的模式吗?
- python - 其他声明表现得好像它不存在
- quip - Quip API - 我可以使用个人访问令牌访问线程/创建文档吗?
- python - 如何从 Selenium 获取属性附加列表?
- php - 如何使用 PHP 按升序键对数组进行排序或排序?
- java - 我需要将这些数据分成自己的集合,以便我可以在需要时调用它们