2

I developed a program in python to plot the following parametric function:

enter image description here

with 0<= u,v <= 2pi and r = 1. Here the code

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.

Domenico
  • 101
  • 7

1 Answers1

1

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. 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()
Domenico
  • 101
  • 7