Skip to content Skip to sidebar Skip to footer

I Have A Problem With Plotting Sphere And A Curve On It

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 sp

Solution 1:

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.

Solution 2:

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

Post a Comment for "I Have A Problem With Plotting Sphere And A Curve On It"