简体   繁体   English

如何在 python 中使用绘制的 3D 参数函数创建动画

[英]How to create an animation using a plotted 3D parametric function in python

I developed a program in python to plot the following parametric function:我在 python 中开发了一个程序来绘制以下参数函数:

在此处输入图片说明

with 0<= u,v <= 2pi and r = 1. Here the code 0<= u,v <= 2pi 和 r = 1. 这里的代码

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
r=1
fig=plt.figure()
ax=fig.gca(projection='3d')
u,v=numpy.mgrid[0:2*numpy.pi:100j,0:2*numpy.pi:100j]
x=(r+numpy.cos(u/2)*numpy.sin(v)-numpy.sin(u/2)*numpy.sin(2*v))*numpy.cos(u)
y=(r+numpy.cos(u/2)*numpy.sin(v)-numpy.sin(u/2)*numpy.sin(2*v))*numpy.sin(u)
z=numpy.sin(u/2)*numpy.sin(v)+numpy.cos(u/2)*numpy.sin(2*v)
ax.plot_wireframe(x,y,z,color='b')
plt.show()

Now I would like to create an animation in order to rotate the surface around the axis z of the sequence phi=i2*pi/360 where i=1, .... 360. I think that I should use the 'matplotlib.animation.funcAnimation' function but I don't know how to invoke it with the parametric functions.现在我想创建一个动画,以便围绕序列 phi=i2*pi/360 其中 i=1, .... 360 的 z 轴旋转表面。我认为我应该使用 'matplotlib.animation .funcAnimation' 函数,但我不知道如何使用参数函数调用它。

I solved the problem defining a function containing the rotation of x and y and passing it to the function funcAnimation belonging to the library matplotlib.animation.我解决了定义一个包含 x 和 y 旋转的函数并将其传递给属于库 matplotlib.animation 的函数 funcAnimation 的问题。 Following the updated snippet code.遵循更新的片段代码。

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
r=1
fig=plt.figure()
ax=fig.gca(projection='3d')
u,v=numpy.mgrid[0:2*numpy.pi:50j,0:2*numpy.pi:50j]
x=(r+numpy.cos(u/2)*numpy.sin(v)-numpy.sin(u/2)*numpy.sin(2*v))*numpy.cos(u)
y=(r+numpy.cos(u/2)*numpy.sin(v)-numpy.sin(u/2)*numpy.sin(2*v))*numpy.sin(u)
z=numpy.sin(u/2)*numpy.sin(v)+numpy.cos(u/2)*numpy.sin(2*v)
wframe = None
ax.set_xlim3d(-2,2)
ax.set_ylim3d(-2,2)
def fun(i):
    global wframe
    if wframe:
       ax.collections.remove(wframe)
    theta=(i*2*numpy.pi)/360
    x1=x*numpy.cos(theta)-y*numpy.sin(theta)
    y1=x*numpy.sin(theta)+y*numpy.cos(theta)
    wframe= ax.plot_wireframe(x1,y1,z,color='r')
ani = animation.FuncAnimation(fig, fun, 360, interval=1)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM