gpt4 book ai didi

python - 动画 matplotlib 参数化示例

转载 作者:行者123 更新时间:2023-12-01 08:42:59 30 4
gpt4 key购买 nike

为 Python 制作动画的简单方法是什么 matlabplot parametric example

我想逐项更新循环内的数据。可悲的是,这看起来很糟糕,而且总是闪烁着彩虹的每种颜色!有没有一种简单的方法可以让我在计算数据时更新数据?

from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import

import numpy as np
import matplotlib.pyplot as plt
import math

plt.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')
i = 10
theta = 0
x=[]
y=[]
z=[]
# Prepare arrays x, y, z
while (theta < 4*np.pi):
theta += 0.05
z += [i]
r = i**2 + 1
x += [r * math.sin(theta)]
y += [r * math.cos(theta)]
i +=1

ax.plot(x, y, z)
plt.pause(0.01)

最佳答案

免责声明,这使用了我编写的一个名为 celluloid 的库。在另一个answer中对其优点进行了一些讨论。 。话虽这么说,这里基本上是您的代码,其中散布着一些赛璐珞行:

import matplotlib
matplotlib.use('Agg')
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import math
from celluloid import Camera

plt.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
camera = Camera(fig)
i = 10
x=[]
y=[]
z=[]
# Prepare arrays x, y, z
for theta in np.arange(0, 4*np.pi, 0.05):
z += [i]
r = i**2 + 1
x += [r * math.sin(theta)]
y += [r * math.cos(theta)]
i +=1
ax.plot(x, y, z, color='blue')
camera.snap()
anim = camera.animate(blit=False, interval=10)
anim.save('3d.mp4')

可以将其变成这样的 gif:

enter image description here

关于python - 动画 matplotlib 参数化示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53435352/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com