The parametric description of a torus with radius c and tube radius a is
xyz=(c+acosθ)cosϕ=(c+acosθ)sinϕ=asinθ
for θ and ϕ each between 0 and 2π. The code below outputs two views of a torus rendered as a surface plot.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
n =100
theta = np.linspace(0,2.0* np.pi, n)
phi = np.linspace(0,2.0* np.pi, n)
theta, phi = np.meshgrid(theta, phi)
c, a =2,1
x =(c + a * np.cos(theta))* np.cos(phi)
y =(c + a * np.cos(theta))* np.sin(phi)
z = a * np.sin(theta)
fig, axes = plt.subplots(nrows=1, ncols=2, subplot_kw={"projection":"3d"})
ax1, ax2 = axes
ax1.set_zlim(-3,3)
ax1.plot_surface(x, y, z, rstride=5, cstride=5, color="k", edgecolors="w")
ax1.view_init(36,26)
ax2.set_zlim(-3,3)
ax2.plot_surface(x, y, z, rstride=5, cstride=5, color="k", edgecolors="w")
ax2.view_init(0,0)
ax2.set_xticks([])
plt.show()
We need θ and ϕ to range over the interval (0,2π) independently, so use a meshgrid.
Note that we can use keywords such as edgecolors to style the polygon patches created by ax.plot_surface.
Elevation angle above the xy-plane of 36∘, azimuthal angle in the xy-plane of 26∘.
Two views of a torus: (a) θ=36∘, ϕ=26∘, (b) θ=0∘, ϕ=0∘