如何连接 3D 空间中的散点?

Hig*_*iga 1 python 3d plot matplotlib

我想在 matplotlib 中将所有弧的起点和终点连接在一起。红色弧是周围所有四个绿色弧的中心。下面您将找到绘制平行弧的代码。

from mpl_toolkits.mplot3d import Axes3D
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

radius1 = 7.5
radius2 = 12.5

# discretize for plotting
arcIndex = np.linspace(0, theta, num = 100)


X, Y, Z, = sphere2cart(radius, arcIndex, phi)

X1, Y1, Z1, = sphere2cart(radius2, arcIndex, phi)
X2, Y2, Z2, = sphere2cart(radius1, arcIndex, phi)
X3, Y3, Z3, = sphere2cart(radius1, arcIndex, phi)
X4, Y4, Z4, = sphere2cart(radius2, arcIndex, phi)


# move center or arc to xy plane
# =============================================================================
x1, y1 = pol2cart(radius, phi) #arc
X += x1
Y += y1
x2, y2 = pol2cart(radius, phi) #L1 
X1 += x2
Y1 += 1.5+y2
x3, y3 = pol2cart(radius, phi) #L2 
X2 += x3
Y2 += y3-1.5
x4, y4 = pol2cart(radius, phi) #L3 
X3 += x4
Y3 += 1.5+y4
x5, y5 = pol2cart(radius, phi) #L4 
X4 += x5
Y4 += y5-1.5
# =============================================================================

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

# plot arc
ax.plot(X, Y, Z, c= "red", label='arc')
ax.plot(X1, Y1, Z1, c= "Green", label='L1')
ax.plot(X2, Y2, Z2, c= "Green", label='L2')
ax.plot(X3, Y3, Z3, c= "Green", label='L3')
ax.plot(X4, Y4, Z4, c= "Green", label='L4')


# 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 = 'Green', label = "center of circle", s=10)

# plot endpoint
# =============================================================================
ax.scatter(X[0], Y[0], Z[0], color="red", label= "origin", s=30)
ax.scatter(X[-1], Y[-1], Z[-1], color= 'red', label = "endpoint", s=30)
ax.scatter(X1[-1], Y1[-1], Z1[-1], color= 'red', s=30)
ax.scatter(X2[-1], Y2[-1], Z2[-1], color= 'red', s=30)
ax.scatter(X3[-1], Y3[-1], Z3[-1], color= 'red', s=30)
ax.scatter(X4[-1], Y4[-1], Z4[-1], color= 'red', s=30)
ax.scatter(X1[1], Y1[1], Z1[1], color= 'red', s=30)
ax.scatter(X2[1], Y2[1], Z2[1], color= 'red', s=30)
ax.scatter(X3[1], Y3[1], Z3[1], color= 'red', s=30)
ax.scatter(X4[1], Y4[1], Z4[1], color= 'red', 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()
Run Code Online (Sandbox Code Playgroud)

我从下面显示的代码中得到的结果, 在此输入图像描述

我试图将所有这些红色散点连接在一起,以便它们可以在弧线的起点和终点形成一个正方形。我想要的是如下图所示的东西,在此输入图像描述

如何将空间中的每个点与下一个点连接起来,使其成为平滑的线图?

Joe*_*Joe 5

我建议根本不要使用 Matplotlib 进行 3D 绘图。你迟早会碰壁。尽管周围有一些看起来很酷的图,但它效果不佳,尤其是当您想要可视化包含许多数据点的散点图时。如果你想绘制彩色的三角形网格或复杂的曲面,它肯定会失败。

请改用以下工具包之一: