动画Matplotlib参数示例

时间:2018-11-22 16:49:24

标签: python animation matplotlib curve

SIMPLIST为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)

1 个答案:

答案 0 :(得分:0)

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

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