1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams['animation.ffmpeg_path'] = 'c:\users\victor\appdata\local\programs\python\python39\lib\site-packages\ffmpeg.exe'
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection="3d")
def init():
k=300
Z = [i for i in range(k)]
X = [math.cos(i/5)*(k-i) for i in range(k)]
Y = [math.sin(i/5)*(k-i) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
step = 3
c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
Z = [i for i in range(1,k,step)]
X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c=c, marker="o",s=40)
plt.xlim(-500,500)
plt.ylim(-500,500)
return fig,
def animate(f):
fig.clear()
ax = fig.add_subplot(111, projection="3d")
k=300
Z = [i for i in range(k)]
X = [math.cos(i/5+f/10)*(k-i) for i in range(k)]
Y = [math.sin(i/5+f/10)*(k-i) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
step = 3
c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
Z = [i for i in range(1,k,step)]
X = [math.cos(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
Y = [math.sin(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c="red", marker="o",s=40)
plt.xlim(-500,500)
plt.ylim(-500,500)
return fig,
ani=animation.FuncAnimation(fig, animate, init_func=init, frames=90,
interval=50, blit=False)
DPI=90
writer = animation.FFMpegWriter(fps=30, bitrate=5000)
ani.save("test.mp4", writer = writer, dpi=DPI) |
Partager