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

旧街凉风 提交于 2021-02-08 23:43:22

问题


I am trying to plot a curve on a sphere but I can not plot them at the same time. I identified some points with Euclidean norm 10 for my curve, and some other points to plot the sphere of radius 10, respectively as following.

Points for curve:

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))]

Where xvalues, yvalues and zvalues are our points Euclidean components.

Points for sphere:

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))

Where x,y and z are Euclidean components of sphere points.

My problem:

When I try to plot the curve, without plotting sphere, it works. But when I plot them together, then it just return the sphere.

The whole code is the following:

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()

What I want to occur:

I would like to plot the curve on the sphere, but it dose not happen in my code. I appreciate any hint.


回答1:


Using spherical coordinates, you can easily do that:

## 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()




回答2:


If you use a "." option for plotting the points, like

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

you will see the points on top of the sphere for certain viewing angles, but disappear sometimes even if they are in front of the sphere. This is a known bug explained in the 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.”

In the same doc, the developers recommend to use Mayavi for more advanced use of 3D plots in Python.



来源:https://stackoverflow.com/questions/64051845/i-have-a-problem-with-plotting-sphere-and-a-curve-on-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!