How do you correctly plot for a 3d graph because my code seems to be stuck in an infinite loop because it is unable to plot.

Joined
Jul 20, 2023
Messages
1
Reaction score
0
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

l = np.array([])
x = int(input("Input starting x coordinate: "))
y = int(input("Input starting y coordinate: "))
z = int(input("Input starting z coordinate: "))
l = np.array([x, y, z])

x_data = (0, l[0] + 0.00001)
y_data = (0, l[1] + 0.00001)
z_data = (0, l[2] + 0.00001)

print(x_data)
print(y_data)
print(z_data)

ax.set_title("Matrix Rotation")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

ac = np.array([l[0], l[1], l[2]])

ax.plot(x_data, y_data, z_data)
plt.show()

while True:
angl = int(input("What angle would you like to rotate it by? "))
answer = input("Would you like to rotate the line around the x, y, or z axis? (Enter 'x', 'y', or 'z') ")
if answer.lower() == "x":
matx = np.array([[1, 0, 0], [0, np.cos(np.degrees(angl)), -np.sin(np.degrees(angl))], [0, np.sin(np.degrees(angl)), np.cos(np.degrees(angl))]])
newcoordinates = np.dot(ac, matx)
print(newcoordinates)
elif answer.lower() == "y":
maty = np.array([[np.cos(np.degrees(angl)), 0, np.sin(np.degrees(angl))], [0, 1, 0], [-np.sin(np.degrees(angl)), 0, np.cos(np.degrees(angl))]])
newcoordinates = np.dot(ac, maty)
elif answer.lower() == "z":
matz = np.array([[np.cos(np.degrees(angl)), -np.sin(np.degrees(angl)), 0], [np.sin(np.degrees(angl)), np.cos(np.degrees(angl)), 0], [0, 0, 1]])
newcoordinates = np.dot(ac, matz)
print(newcoordinates)
else:
print("Invalid input. Please enter 'x', 'y', or 'z'.")
continue

print("Out of if conditional")

x_data = np.array([0, newcoordinates[0] + 0.000001])
y_data = np.array([0, newcoordinates[1] + 0.000001])
z_data = np.array([0, newcoordinates[2] + 0.000001])

ac = np.array([newcoordinates[0], newcoordinates[1], newcoordinates[2]])

ax.clear()
ax.set_title("Matrix Rotation")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

ax.plot(x_data, y_data, z_data)
plt.ion()

plt.pause(0.001)

plt.show()
 
Joined
Jul 28, 2023
Messages
2
Reaction score
0
Hi
Check if this works for you.
Python:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


# ax.plot(x_data, y_data, z_data)
# plt.show()

def change_matrix(x, y, z, newcoordinates):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    l = np.array([x, y, z])

    x_data = (0, l[0] + 0.00001)
    y_data = (0, l[1] + 0.00001)
    z_data = (0, l[2] + 0.00001)

    print(x_data)
    print(y_data)
    print(z_data)

    ax.set_title("Matrix Rotation")
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("z")

    ac = np.array([l[0], l[1], l[2]])
    x_data = np.array([0, newcoordinates[0] + 0.000001])
    y_data = np.array([0, newcoordinates[1] + 0.000001])
    z_data = np.array([0, newcoordinates[2] + 0.000001])

    ac = np.array([newcoordinates[0], newcoordinates[1], newcoordinates[2]])

    ax.clear()
    ax.set_title("Matrix Rotation")
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("z")

    ax.plot(x_data, y_data, z_data)
    plt.ion()

    plt.pause(0.001)

    plt.show()
    
x = int(input("Input starting x coordinate: "))
y = int(input("Input starting y coordinate: "))
z = int(input("Input starting z coordinate: "))
while True:
    answer = input("Would you like to rotate the line around the x, y, or z axis? (Enter 'x', 'y', or 'z') or end? (Enter) ")
    if answer.lower() not in [ "x", "y", "z"]:
        break
    angl = int(input("What angle would you like to rotate it by? "))
    if answer.lower() == "x":
        matx = np.array([[1, 0, 0], [0, np.cos(np.degrees(angl)), -np.sin(np.degrees(angl))], [0, np.sin(np.degrees(angl)), np.cos(np.degrees(angl))]])
        newcoordinates = np.dot(ac, matx)
        print(newcoordinates)
        change_matrix(x, y, z, newcoordinates)
    elif answer.lower() == "y":
        maty = np.array([[np.cos(np.degrees(angl)), 0, np.sin(np.degrees(angl))], [0, 1, 0], [-np.sin(np.degrees(angl)), 0, np.cos(np.degrees(angl))]])
        newcoordinates = np.dot(ac, maty)
        print(newcoordinates)
        change_matrix(x, y, z, newcoordinates)
    elif answer.lower() == "z":
        matz = np.array([[np.cos(np.degrees(angl)), -np.sin(np.degrees(angl)), 0], [np.sin(np.degrees(angl)), np.cos(np.degrees(angl)), 0], [0, 0, 1]])
        newcoordinates = np.dot(ac, matz)
        print(newcoordinates)
        change_matrix(x, y, z, newcoordinates)
    else:
        print("Invalid input. Please enter 'x', 'y', or 'z'.")
        break
    continue

print("Out of if conditional")
 
Joined
Sep 4, 2022
Messages
129
Reaction score
16
"if elif chain" are often 'hard to understand', 'hard to read', 'hard to maintain';

you can replace them , by several : "if"
because you have well clean and safe tests of your values.


you can replace your while(true) by : while (
input("Would you like to rotate the line around the x, y, or z axis? (Enter 'x', 'y', or 'z') or end? (Enter) ").lower() in [ "x", "y", "z"] )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,355
Latest member
SJLChristi

Latest Threads

Top