Draw knots interactively in Plotly (Python)

Draw knots interactively in Plotly (Python)

1.First of all

This time, I will draw a knot in Plotly on a three-dimensional space. I hope it will help you in your studies.

You can do something like ↓.

See the Pen dyoyxKW by Sota Misawa (@mitawaut) on CodePen.

Source (GitHub)

2. Review

I will do the minimum necessary review to illustrate the knot.

A ** knot ** (knot) is a $ S ^ 1 $ piecewise embedded in $ S ^ 3 $. Since $ S ^ 3 $ can be regarded as $ \ mathbb {R} ^ 3 \ cup \ {\ infinty \} $, if the embedded $ S ^ 1 $ does not include $ \ infinty $, then $ \ You can think of it as $ S ^ 1 $ embedded in mathbb {R} ^ 3 $.

A well-known method of expressing knots is a knot along the two-dimensional torus surface $ \ mathbb {T} ^ 2 . Consider the closed surface defined by the following map as a torus: $\mathbb{T}^2 : [-\pi , \pi]^2 \ni (\phi,\ \theta)\longmapsto \bigl(\cos \phi \ (3+\cos \theta),\ \sin \phi \ (3+\cos \theta),\ -\sin \theta \bigr)\in \mathbb{R}^3$$

torus.png

Using a pair of integers $ (n, \ m) $ on a closed curve on $ \ mathbb {T} ^ 2 $ (extending the domain of $ \ mathbb {T} ^ 2 $ appropriately), $ T(n,\ m) : [-\pi , \pi]\ni t \longmapsto \mathbb{T}^2(nt,\ mt) \in \mathbb{R}^3$

Consider what is represented as. You can think of this as a knot. The following theorems are known in this regard.

** Theorem. ** For any non-trivial knot $ K $ contained in $ \ mathbb {T} ^ 2 $, there exists a relatively prime integer pair $ (n, \ m) $ for $ K. $ Is equivalent to $ T (n, \ m) $.

From this theorem, we try to illustrate $ T (n, \ m) $ as a knot expression. Below is a diagram of $ T (2, \ 3) $. knot_on_torus.png

3. Implementation

This is the beginning of cooking for 5 minutes. Since ipywidgets is used, jupyter notebook is recommended.

requirements.txt[Excerpt]


ipython==7.12.0
numpy==1.18.1
plotly==4.4.1

The goal is to be able to manipulate $ n, \ m $ with ipywidgets. First, import what you need.


# import modules
import plotly.offline as offline
import plotly.graph_objs as go
import numpy as np
from ipywidgets import interactive, VBox, widgets
from IPython.display import display

offline.init_notebook_mode(connected=True)

Then define $ \ mathbb {T} ^ 2 $ and $ T (n, \ m) $. Again, this just implements the above discussion as is.

# functions
def torus(p, t):
    x = np.cos(p) * (3 + np.cos(t))
    y = np.sin(p) * (3 + np.cos(t))
    z = -np.sin(t)
    return (x, y, z)

def knot(n, m):
    theta = np.linspace(-np.pi, np.pi, 1000)
    p, t = n*theta, m*theta
    return torus(p, t)

All you have to do is plot. The initial value is $ (m, \ m) = (2, \ 3) $, and the slide bar range is $ n $: -10 ~ 10, $ m $: 0 ~ 10.

fig = go.FigureWidget()
scatt = fig.add_scatter3d()

def update(n=2, m=3):
    with fig.batch_update():
        (cx, cy, cz) = knot(n, m)
        scatt.data[0].x=cx
        scatt.data[0].y=cy
        scatt.data[0].z=cz
        scatt.data[0].mode='lines'
        scatt.data[0].line=dict(
                    width=10,
                    color='green')

vb = VBox((fig, interactive(update, n=(-10, 11, 1), m=(0, 10, 1))))
display(vb)

If the following is displayed and the knot changes at the same time as you play with the slide bar, you are successful. knot_2_3.png

4. Finally

If you don't see it, check the version around ipython. ~~ I thought it was awkward for the first post to be of a few minutes quality, so I created an extra section called review ~~

Recommended Posts

Draw knots interactively in Plotly (Python)
Draw graph in python
Draw mp3 waveform in Python
Draw Poincare's disk in Python
Draw "Draw Ferns Programmatically" in Python
Draw implicit function in python
Draw a heart in Python
Draw Sine Waves in Blender Python
Draw a scatterplot matrix in python
Draw a CNN diagram in Python
Draw Nozomi Sasaki in Excel with python
Draw a heart in Python Part 2 (SymPy)
Draw a tree in Python 3 using graphviz
Displaying candlestick charts in Python (Plotly edition)
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Draw a graph of a quadratic function in Python
Draw graphs in Julia ... Leave the graphs to Python
[Python] How to draw a histogram in Matplotlib
Sorted list in Python