我在绘制球体和上面的曲线时遇到问题

I have a problem with plotting sphere and a curve on it

我正在尝试在球体上绘制曲线,但无法同时绘制它们。我用 Euclidean norm 10 为我的曲线确定了一些点,还有一些点用于绘制 radius 10[= 的球体45=],分别如下

曲线点数:

random_numbers=[]
basevalues=np.linspace(-0.9,0.9,100)
for i in range(len(basevalues)):
    t=random.random()
    random_numbers.append(t*10)
    
xvalues=[random_numbers[i]*np.cos(basevalues[i]) for i in range(len(basevalues))]
yvalues=[random_numbers[i]*np.sin(basevalues[i]) for i in range(len(basevalues))]
zvalues=[np.sqrt(100-xvalues[i]**2-yvalues[i]**2)for i in range(len(basevalues))]

其中 xvaluesyvalueszvalues 是我们的点欧氏分量。

球体点数:

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

其中 x,yz 是球面点的欧氏分量。

我的问题:

当我尝试绘制曲线而不绘制球体时,它起作用了。但是当我将它们绘制在一起时,它只是 return 球体。

整个代码如下:

import matplotlib.pyplot as plt
import numpy as np
import random


#Curve points

random_numbers=[]
basevalues=np.linspace(-0.9,0.9,100)
for i in range(len(basevalues)):
    t=random.random()
    random_numbers.append(t*10)
    
xvalues=[random_numbers[i]*np.cos(basevalues[i]) for i in range(len(basevalues))]
yvalues=[random_numbers[i]*np.sin(basevalues[i]) for i in range(len(basevalues))]
zvalues=[np.sqrt(100-xvalues[i]**2-yvalues[i]**2)for i in range(len(basevalues))]


# Sphere points

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

# Plot the surface and curve 

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
circ = ax.plot(xvalues,yvalues,zvalues, color='green',linewidth=1)
sphere=ax.plot_surface(x, y, z, color='r')


ax.set_zlim(-10, 10)
plt.xlabel("X axes")
plt.ylabel("Y axes")
plt.show()

我要发生的事情:

我想在球体上绘制曲线,但在我的代码中没有发生。我很感激任何提示。

如果您使用 "." 选项来绘制点,例如

circ = ax.plot(xvalues, yvalues,zvalues, '.', color='green', linewidth=1)

您会在某些视角看到球体顶部的点,但有时即使它们在球体前面也会消失。这是 matplotlib documentation:

中解释的已知错误

My 3D plot doesn’t look right at certain viewing angles: This is probably the most commonly reported issue with mplot3d. The problem is that – from some viewing angles – a 3D object would appear in front of another object, even though it is physically behind it. This can result in plots that do not look “physically correct.”

在同一文档中,开发人员建议使用 Mayavi 以便在 Python 中更高级地使用 3D 图。

使用球坐标,您可以轻松做到这一点:

## plot a circle on the sphere using spherical coordinate.
import numpy as np
import matplotlib.pyplot as plt

# a complete sphere
R = 10
theta = np.linspace(0, 2 * np.pi, 1000)
phi = np.linspace(0, np.pi, 1000)
x_sphere = R * np.outer(np.cos(theta), np.sin(phi))
y_sphere = R * np.outer(np.sin(theta), np.sin(phi))
z_sphere = R * np.outer(np.ones(np.size(theta)), np.cos(phi))

# a complete circle on the sphere
x_circle = R * np.sin(theta)
y_circle = R * np.cos(theta)

# 3d plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x_sphere, y_sphere, z_sphere, color='blue', alpha=0.2)
ax.plot(x_circle, y_circle, 0, color='green')
plt.show()