问题描述
我正在尝试在球体上绘制曲线,但无法同时绘制它们。我为曲线确定了一些欧几里得范数 10 的点,另外一些点分别绘制了半径10 的球面。 / p>
曲线点:
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))]
其中 xvalues , yvalues 和 zvalues 是我们的欧几里得积分成分。
球形点
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,y 和 z 是球点的欧几里得分量。
我的问题:
当我尝试绘制曲线而不绘制球体时,它可以工作。但是当我将它们绘制在一起时,它将返回球体。
整个代码如下:
import matplotlib.pyplot as plt
import numpy as np
import random
#Curve points
random_numbers=[]
basevalues=np.linspace(-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,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中解释的已知错误:
我的3D图在某些视角下看起来不正确: 这可能是mplot3d最常报告的问题。问题是-从某些角度看-3D对象会出现在另一个对象的前面,即使它实际上在后面。这可能会导致绘图看起来“物理上不正确”。
在同一文档中,开发人员建议使用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,color='green')
plt.show()