In [1]:
import plotly.offline as py
from plotly.graph_objs import *
import numpy as np
from numpy import pi, cos, sin, exp, log, sqrt
py.init_notebook_mode()
In [2]:
def snake(u,v):
    lu, lv = len(u), len(v)
    path = []

    i, j = 0, 0
    istep, jstep = 1, 1
    while(i < lu):
        while(0 <= j < lv):
            path.append((u[i],v[j]))
            j += jstep
        j -= jstep
        i += istep
        jstep *= -1

    i -= istep
    i -= istep
    istep *= -1
    while(0 <= j < lv):
        while(0 <= i < lu):
            path.append((u[i],v[j]))
            i += istep
        i -= istep
        j += jstep
        istep *= -1
    return path
In [3]:
def surface(rfun,tmin=-2,tmax=2,tpts=20,umin=-2,umax=2,upts=20,color='black'):
    t, u = np.linspace(tmin,tmax,tpts), np.linspace(umin,umax,upts)
    path = snake(t,u)
    r = [[rfun(t,u)[i] for (t,u) in path] for i in range(3)]
    trace = Scatter3d(x=r[0],y=r[1],z=r[2],mode='lines',
                      line=Line(color=color,width=3))
    return(trace)
In [5]:
r=2
R=5
torus = surface(lambda t,u: ((R+r*cos(t))*cos(u),(R+r*cos(t))*sin(u),r*sin(t)),
                tpts=40,upts=40,tmin=0,umin=0,tmax=2*pi,umax=2*pi,color='brown')
py.iplot(Figure(data=Data([torus])))
In [8]:
h=2*pi
s = surface(lambda t,u: ((3+sin(t))*(2+cos(u)),(4+3*cos(t))*(5+cos(u)),7+2*sin(t+u)),
                tmin=0,tmax=h,umin=0,umax=h,color='black',tpts=40,upts=40)
py.iplot(Figure(data=Data([s])))
In []:
 
In []:
 
In []:
 
In []:
 
In []: