2

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. enter image description here

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

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

sodd
  • 12,482
  • 3
  • 54
  • 62
freethebees
  • 957
  • 10
  • 24
  • Can you please include link to figure (as you don't have enough rep to upload), so we can see what your output looks like? It's not real easy to understand what you mean, at least when the data to replicate the result is not present. – sodd Jun 06 '13 at 10:56
  • I've included a couple of links to images at the end now. – freethebees Jun 06 '13 at 11:05
  • 2
    Matplotlib is mainly a 2D plotting library, whereas the 3D plotting is done by projections and transformations. For this reason matplotlib draws in _layers_ rather than "real" 3D; what layer something is drawn on, depends on the `zorder` argument. As a result, the lines shown at the top of your sphere surface, cannot be _both_ in front of the sphere and behind the sphere simultaneously. If you really need 3D plotting capabilities, check out [mayavi](http://code.enthought.com/projects/mayavi/) from Enthought. – sodd Jun 06 '13 at 11:49
  • Thanks. I use Canopy so I'll get onto that right away. – freethebees Jun 06 '13 at 13:26
  • 1
    A work around to the issues @nordev describes is to split your surfaces/lines up 'by hand'. See http://stackoverflow.com/questions/14824893/how-to-draw-diagrams-like-this/14825951#14825951 – tacaswell Jun 06 '13 at 14:06

0 Answers0