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
  • 91
  • 7
  • Rotate the function or the viewpoint? Both are easy but there's a huge difference. – Andras Deak Jan 02 '20 at 14:30
  • Rotate the function, not the viewpoint. I want to create an animation with the sequence of the angle phi=i*2*pi/360 with i=1,....,360 – Domenico Jan 02 '20 at 14:32
  • In that case transform your x and y with the corresponding 2d rotation matrix with angle `delta_phi`. – Andras Deak Jan 02 '20 at 14:35
  • See e.g. https://stackoverflow.com/questions/6802577/rotation-of-3d-vector but rotating around z, as I said, is even simpler. – Andras Deak Jan 02 '20 at 14:39
  • I want to create an animation of the graph, similar to this https://stackoverflow.com/questions/45712099/updating-z-data-on-a-surface-plot-in-matplotlib-animation or https://matplotlib.org/2.1.0/gallery/animation/simple_3danim.html – Domenico Jan 02 '20 at 14:41
  • Part 1: how do I create an animation given a plotting step? Part 2: how do I plot a function that's rotated with a given angle? Seems straightforward to me to combine the two. – Andras Deak Jan 02 '20 at 14:46

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
  • 91
  • 7