I am trying to plot the Permutohedron in python using plotly, numpy, and pandas.
This is my current code:
import plotly.express as px
import numpy as np
import itertools
import pandas as pd
order = 4
items = range(1, order+1)
permuted_items = np.array([*itertools.permutations(items)])
def closest_nodes(node, nodes):
# Returns the instances in nodes that are closest to node
nodes = np.asarray(nodes)
dist_2 = np.sum((nodes - node)**2, axis=1)**.5
indices = np.where(dist_2 == dist_2.min())[0]
return nodes[indices]
xyzs = []
colors = []
for i, point in enumerate(permuted_items[:-1]):
closest_points = closest_nodes(point, permuted_items[i+1:])
for c_point in closest_points:
xyzs.extend([point[:3], c_point[:3]])
# Get unique string as color to group above line while plotting
c = str(point) + str(c_point[:3])
colors.extend([c, c])
lines = np.array(xyzs)
x, y, z = lines.T
plotting_data = pd.DataFrame({
"X": x,
"Y": y,
"Z": z,
"color": colors
})
fig = px.line_3d(plotting_data, x='X', y='Y', z='Z', color="color")
fig.show()
But this plots something that is very skewed:
I.e. my way of showing this shape in 3d (by removing the last dimension) changes the length of each line such that each line does not have a length of sqrt(2).
In reality, this is the shape I am after:
Any help?