Plotting a 3D trefoil knot
In this recipe, we will see how to plot a 3D trefoil knot. A trefoil knot is a closed curve with three crossings. In this recipe, we will draw not just a curve, but a solid 3D curve. This is beyond the plotly capabilities, and we will implement this functionality using a trick.
How to do it...
In this recipe, we will:
Generate all the points of the knot using parametric equations.
Organize the data as required by
plotly
.Define a clean layout.
Invoke
plotly
to draw the chart:import numpy as np import plotly.plotly as py from plotly.graph_objs import Scatter3d, Data, Layout from plotly.graph_objs import Figure, Line, Margin, Marker from numpy import linspace,pi,cos,sin phi = linspace(0,2*pi,250) x = sin(phi)+2*sin(2*phi) y = cos(phi)-2*cos(2*phi) z = -sin(3*phi) traces = list() colors = ['rgb(%d,50,210)' % c for c in np.abs(z / max(z)) * 255] for i in linspace(-np.pi,np.pi,50): trace = Scatter3d(x=x+np.cos(i)*.5, y=y+np.sin(i)*.5, z=z, mode...