Codementor Events

Python/matplotlib : How can I draw 4 parallel line those are at equal distance from center line?

Published Aug 07, 2019

I'm attempting to draw four parallel lines (green lines), those are at equal distance from the center line. The center line (red line) must be the square's center line. You can look at the picture for more clarification.

20190807_110820.jpg

I’ve tried a code, but I just received the center line with this code. The code can be found below.

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import interactive
interactive(True)


def cart2sphere(x, y, z):
    r = np.sqrt(x**2 + y**2 + z**2)
    theta = np.arccos(z, r)
    phi = np.arctan2(y, x)
    return(r, theta, phi)

def sphere2cart(r, theta, phi):
    theta = theta - np.pi/2
    x = r * np.sin(theta)* np.cos(phi)
    y = r * np.sin(theta)* np.sin(phi)
    z = r * np.cos(theta)
    return(x, y, z)

def pol2cart(rho, phi):
    x = rho * np.cos(phi)
    y = rho * np.sin(phi)
    return(x, y)
    
# define values 
theta = np.pi/2 # arclength in radians
radius = 10 # raduis of circle
k = 1/radius # if you want to use k instead of radius
phi = np.pi/6 # angle of circle in xy plane

# discretize for plotting
arcIndex = np.linspace(0, theta, num = 100)
X, Y, Z, = sphere2cart(radius, arcIndex, phi)


# move center or arc to xy plane
x1, y1 = pol2cart(radius, phi)
X += x1
Y += y1

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

# plot arc
ax.plot(X, Y, Z, c= "red", label='arc')

# plot axes
ax.plot(np.zeros(100), np.zeros(100), np.linspace(-np.max(np.abs(Z)), np.max(np.abs(Z)), 100), c= "black", alpha = 0.15)
ax.plot(np.zeros(100), np.linspace(-np.max(np.abs(Z)), np.max(np.abs(Z)), 100), np.zeros(100),  c= "black", alpha = 0.15)
ax.plot(np.linspace(-np.max(np.abs(Z)), np.max(np.abs(Z)), 100), np.zeros(100), np.zeros(100),  c= "black", alpha = 0.15)

# plot center of circle
ax.scatter(np.array([x1]), np.array([y1]), np.array([0]), c = 'orange', label = "center of circle", s=30)

# plot endpoint
ax.scatter(X[-1], Y[-1], Z[-1], color= 'blue', label = "endpoint", s=30)
ax.scatter(X[0], Y[0], Z[0], color="green", label= "origin", s=30)

# plot projection on each axis
ax.plot(X, np.zeros(len(X)), np.zeros(len(X)), color = "blue", label = "X projection")
ax.plot(np.zeros(len(X)), Y, np.zeros(len(X)), color = "green", label = "Y projection")
ax.plot(np.zeros(len(X)), np.zeros(len(X)), Z, color = "brown", label = "Z projection")

ax.legend()
plt.show()

Resulting 3d plot is,
Figure_2-1.png

What modification should I made in code, to get four parallel lines? It would be great if someone could assist me.

Discover and read more posts from Higa
get started
post commentsBe the first to share your opinion
Show more replies