Python Matplotlib 3D line appearing through surface

ε祈祈猫儿з 提交于 2020-01-04 09:03:37

问题


I am using a mapping to convert points on the surface of a cube to a sphere.

I want to plot the surface of the resulting sphere and lines where the edges of the cube are mapped.

So far, I have tried the following, working on one face of the cube at a time:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def sphere_mapping(xbar,ybar,zbar):
    xx = xbar**2
    yy = ybar**2
    zz = zbar**2
    x = xbar*np.sqrt(1 - yy/2 - zz/2 + yy*zz/3)
    y = ybar*np.sqrt(1 - xx/2 - zz/2 + xx*zz/3)
    z = zbar*np.sqrt(1 - xx/2 - yy/2 + xx*yy/3)
    return x,y,z

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d',aspect='equal')
sgrd1,sgrd2 = np.meshgrid(np.linspace(-1,1,100),np.linspace(-1,1,100))
sgrd3 = np.ones((100,100))
lgrd1 = np.hstack([np.linspace(-1,1,100),np.ones(100,),np.linspace(1,-1,100),-np.ones(100,)])
lgrd2 = np.hstack([np.ones(100,),np.linspace(1,-1,100),-np.ones(100,),np.linspace(-1,1,100)])
lgrd3 = np.ones(400,)

# Plot surface
# x=+1
x,y,z=transform(fgrd3,fgrd1,fgrd2)
ax.plot_surface(x,y,z,linewidth=0)
# y=+1
x,y,z=transform(fgrd1,fgrd3,fgrd2)
ax.plot_surface(x,y,z,linewidth=0)
# x=-1
x,y,z=transform(-fgrd3,fgrd1,fgrd2)
ax.plot_surface(x,y,z,linewidth=0)
# y=-1
x,y,z=transform(fgrd1,-fgrd3,fgrd2)
ax.plot_surface(x,y,z,linewidth=0)
# z=+1
x,y,z=transform(fgrd2,fgrd1,fgrd3)
ax.plot_surface(x,y,z,linewidth=0)
# z=-1    
x,y,z=transform(fgrd2,fgrd1,-fgrd3)
ax.plot_surface(x,y,z,linewidth=0)

# Plot lines
# x=+1
x,y,z=transform(lgrd3,lgrd1,lgrd2)
ax.plot_wireframe(x,y,z,linewidth=4,color='k')
# y=+1
x,y,z=transform(lgrd1,lgrd3,lgrd2)
ax.plot_wireframe(x,y,z,linewidth=4,color='k')
# x=-1
x,y,z=transform(-lgrd3,lgrd1,lgrd2)
ax.plot_wireframe(x,y,z,linewidth=4,color='k')
# y=-1
x,y,z=transform(lgrd1,-lgrd3,lgrd2)
ax.plot_wireframe(x,y,z,linewidth=4,color='k')
# z=+1
x,y,z=transform(lgrd2,lgrd1,lgrd3)
ax.plot_wireframe(x,y,z,linewidth=4,color='k')
# z=-1    
x,y,z=transform(lgrd2,lgrd1,-lgrd3)
ax.plot_wireframe(x,y,z,linewidth=4,color='k')

plt.show()

This works fine to a point. I get a sphere and I get the thick black lines of the edges. However, the lines are showing through the surface of the sphere.

If you change the mapping so that it simply returns xbar,ybar,zbar, you get the original cube and lines appear properly.

Any ideas why my lines are showing through with the sphere and how to fix it?

来源:https://stackoverflow.com/questions/16960126/python-matplotlib-3d-line-appearing-through-surface

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