I would like to draw some points on the surface of a sphere. For my purposes, I need to be able to plot lines around the three dimensional sphere. The sphere shall be opaque, such that in the view one can only see the points on the visible half of the sphere. Currently I am just plotting the points with the following snippet:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 1 * np.outer(np.cos(u), np.sin(v))
y = 1 * np.outer(np.sin(u), np.sin(v))
z = 1 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride =5,
cstride = 5, color ='yellow')
plt.plot(CatVec[:,0],CatVec[:,1],CatVec[:,2],'ro')
This give me the following plot:
Unfortunately, all points are visible. Also those that are actually on the backside of the sphere.
How can I make the sphere opaque, such that only the points on the facing side of the sphere are visible?
This is not easy to do with matplotlib due to the way that the 3D graphs are rendered. There is not true 3D support, there is just code to generate a projection from 3D down to 2D. This works great for single artists and can cover a lot of use cases, however the way that the artist is drawn to the canvas means that one artist is always on top of the other, there is no way to have the artists switch order in some places.
You can sort of work around this by doing the splitting 'by hand' and plotting your functions peice wise with the correct order (see How to draw intersecting planes?), however this means you can not rotate out of some small viewing angle and have things look 'right'.
I would recommend looking into mayavi which is an openGL based plotting package that can do true 3D.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With