3D plotting all possible permutations or possibly combinations of 0-9

Viewed 493

I am attempting to plot all permutations of the numbers 0-9 on a 3D plot. So far I have managed to learn enough to do the basics, but am having a hard time grasping the concept of plotting the permutations for all the X, Y, Z values 0-9. I do understand combinations are different from permutations but was not sure if combinations would be better suited for what I am attempting.

I have so far progressed to achieved the solution below. But being unfamiliar with matplotlib and Python in general. I am still under the impression itertools might be used to make the permutations of 0-9. But I have no idea how to implement it in a scenario as such this.

I would like to make these permutations on the X, Y, Z to be able then to call a function to grab all values on the X, Y, Z plane as specified as a parameter of just 3 values, representing the permutation numbers on intersecting plane by these 3 coordinates. If you were to draw a line from the X, Y, Z position to get the permutation values as a reference.

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

i = 0
# Plotting (X, Y, Z) for range 0-9, but all permutations of (X, Y, Z) 0-9
for x, y, z in zip(range(0, 10), range(0, 10), range(0, 10)):
    i += 1  # Need to show number on plot
    # Plot point, but also display number
    ax.text(x, y, z, i, color="red")

# Set x limiter
ax.set_xlim(0, 10)
# Set y limiter
ax.set_ylim(0, 10)
# Set z limiter
ax.set_zlim(0, 10)

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

# Show plots
plt.show()
1 Answers

Your issue is with zip, I think.

i = 0
for x, y, z in zip(range(0, 10), range(0, 10), range(0, 10)):
    i += 1
    print(x, y, z, i)

gives

0 0 0 1
1 1 1 2
2 2 2 3
3 3 3 4
4 4 4 5
5 5 5 6
6 6 6 7
7 7 7 8
8 8 8 9
9 9 9 10

The simplest way to iterate all combinations of x, y, z is probably:

for x in range(0, 10):
    for y in range(0, 10):
        for z in range(0, 10):
            i += 1

but you may also be interested in itertools.product

for x, y, z in itertools.product(range(0, 10), range(0, 10), range(0, 10)):

https://docs.python.org/3/library/itertools.html#itertools.product

And if you're looking to disallow repeats (ie, no <1,3,1>) https://docs.python.org/3/library/itertools.html#itertools.permutations might be helpful

Related