In [6]:
%matplotlib notebook
from functools import cache
import numpy as np
import matplotlib.pyplot as plt
import itertools
import math
from decimal import Decimal, getcontext
from lib import make_pqzo
from mpl_toolkits import mplot3d
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
In [2]:
def recover(x, y):
    """Given the x y coordinates on the 2-simplex,
    return the coordinate in 3-space corresponding to this point.
    
    Assumes x', y' were calculated as [x, y, z] @ X, and [x, y, z] @ Y, respectively
    where X = [1, -1, 0] and Y = [0, 0, 1]
    """
    Z = y
    X = (x + 1 - y) / 2
    Y = -(x - X)
    return np.array([X, Y, Z])

def project(x, y, z):
    X = np.array([1, -1, 0])
    Y = np.array([0,  0, 1])
    return np.array([[x, y, z] @ X, [x, y, z] @ Y])
In [20]:
# See
# https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html
# https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.html#mpl_toolkits.mplot3d.axes3d.Axes3D
# https://matplotlib.org/stable/api/toolkits/mplot3d.html

X = np.array([1, -1, 0])
Y = np.array([0,  0, 1])

ax = plt.axes(projection='3d')
Z_OFFSET = 100

# Plot triangles
simplex_x = [point @ X for point in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]]
simplex_y = [point @ Y for point in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]]
simplex_x.append(simplex_x[0])
simplex_y.append(simplex_y[0])
simplex_x = np.array(simplex_x)
simplex_y = np.array(simplex_y)
simplex_z = np.zeros(4)
# ax.plot_trisurf(simplex_x, simplex_y, simplex_z)
# ax.plot_trisurf(simplex_x, simplex_y, simplex_z + Z_OFFSET)

points = [Decimal(i) / Decimal(10) for i in range(11)]
for x in points:
    for y in points:
        z = Decimal(1) - x - y
        if z < 0:
            continue
        p, q = make_pqzo(x, y, z)[:2]
        P = project(*(p(i, 0) for i in range(1, 4)))
        Q = project(*(q(i, 0) for i in range(1, 4)))
        x_axis = [float(P[0]), float(Q[0])]
        y_axis = [float(P[1]), float(Q[1])]
        ax.plot3D(x_axis, y_axis, [0, Z_OFFSET], 'gray')
In [25]:
# See
# https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html
# https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.html#mpl_toolkits.mplot3d.axes3d.Axes3D
# https://matplotlib.org/stable/api/toolkits/mplot3d.html

X = np.array([1, -1, 0])
Y = np.array([0,  0, 1])

ax = plt.axes(projection='3d')
Z_OFFSET = 100

# Plot triangles
simplex_x = [point @ X for point in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]]
simplex_y = [point @ Y for point in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]]
simplex_x.append(simplex_x[0])
simplex_y.append(simplex_y[0])
simplex_x = np.array(simplex_x)
simplex_y = np.array(simplex_y)
simplex_z = np.zeros(4)
# ax.plot_trisurf(simplex_x, simplex_y, simplex_z)
# ax.plot_trisurf(simplex_x, simplex_y, simplex_z + Z_OFFSET)

K = 50
points = [Decimal(i) / Decimal(10) for i in range(11)]
for x in points:
    for y in points:
        z = Decimal(1) - x - y
        if z < 0:
            continue
        p, q = make_pqzo(x, y, z)[:2]
        x_axis_p = []
        y_axis_p = []
        x_axis_q = []
        y_axis_q = []
        for k in range(K):
            for fun, x_axis, y_axis in [[p, x_axis_p, y_axis_p], [q, x_axis_q, y_axis_q]]:
                P = project(*(p(i, k) for i in range(1, 4)))
                x_axis.append(float(P[0]))
                y_axis.append(float(P[1]))
        ax.plot3D(x_axis_p, y_axis_p, np.linspace(0, K), 'blue')
        ax.plot3D(x_axis_q, y_axis_q, np.linspace(0, K), 'orange')
In [12]:
getcontext().prec = 1000
X = np.array([1, -1, 0])
Y = np.array([0,  0, 1])

fig, ax = plt.subplots()

points = [Decimal(i) / Decimal(10) for i in range(11)]
lines = []
funcs = []
x_axes = []
y_axes = []
for x in points:
    for y in points:
        z = Decimal(1) - x - y
        if z < 0:
            continue
        p, q = make_pqzo(x, y, z)[:2]
#         P = project(*(p(i, 0) for i in range(1, 4)))
#         Q = project(*(q(i, 0) for i in range(1, 4)))
#         x_axis = [float(P[0]), float(Q[0])]
#         y_axis = [float(P[1]), float(Q[1])]
        x_axes.append([])
        y_axes.append([])
        funcs.append(p)
        lines.append(plt.plot([], [], '-')[0])

def init():
    ax.set_xlim(-1.1, 1.1)
    ax.set_ylim(-.1, 1.1)
    ax.legend()
    return lines

def update(frame):
    for ln, x_axis, y_axis, fun in zip(lines, x_axes, y_axes, funcs):
        point = [fun(1, frame), fun(2, frame), fun(3, frame)]
        x_axis.append(point @ X)
        y_axis.append(point @ Y)
        ln.set_data(x_axis, y_axis)
        # dot.set_data(x_axis[-1], y_axis[-1])
    return lines

ani = FuncAnimation(fig, update, frames=list(range(0, 100)),
                    init_func=init, blit=True)

HTML(ani.to_jshtml())
No handles with labels found to put in legend.
Out[12]: