 # mplot3d and plot_surface: How to 'hide' objects inside the surface

Hi,

I have the following situation. I have been following the example to create a 3d surface, as explained here:

http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo2.html

and I have altered the code slightly to plot a straight red line from the center outside of the sphere (see code at the end of this email). As one can see, the whole line is visible always, no matter how the sphere is turned. Is there a way to ‘hide’ those parts of the red line, which are ‘behind’ the blue surface? Like you stick a pencil into an apple and turn the apple, so you can see parts of the pencil, depending on how the apple is rotated (except the part of the pencil inside the apple)?

If someone can help me with that problem that would be great.

Thanks
Alex

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

fig = plt.figure()
ax = Axes3D(fig)

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

ax.plot([0,15],[0.0,0.0],‘r’, lw=4)

ax.plot_surface(x, y, z, rstride=4, cstride=4, color=‘b’)

plt.show()

Mplot3d is not a true 3d plotting system. I would recommend mayavi
for that. Because mplot3d is a hack to render 3d objects with a 2d
rendering system, matplotlib can't get the the layering right. Maybe
in the far future this will be changed, but for now, it is a design
limitation.

Ben

···

On Thursday, May 5, 2011, Alexander Dietz <alexanderdietz1@...982...> wrote:

Hi,

I have the following situation. I have been following the example to create a 3d surface, as explained here:

http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo2.html

and I have altered the code slightly to plot a straight red line from the center outside of the sphere (see code at the end of this email). As one can see, the whole line is visible always, no matter how the sphere is turned. Is there a way to 'hide' those parts of the red line, which are 'behind' the blue surface? Like you stick a pencil into an apple and turn the apple, so you can see parts of the pencil, depending on how the apple is rotated (except the part of the pencil inside the apple)?

If someone can help me with that problem that would be great.

Thanks
Alex

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

fig = plt.figure()
ax = Axes3D(fig)

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

ax.plot([0,15],[0.0,0.0],'r', lw=4)

ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')

plt.show()

Hi Benjamin,

thanks for your answer - I feared you would reply as you did. I will have a look at mayavi then.

Thanks
Alex

···

On Fri, May 6, 2011 at 00:24, Benjamin Root <ben.root@…1836…304…> wrote:

On Thursday, May 5, 2011, Alexander Dietz > <alexanderdietz1@…982…> wrote:

Hi,

I have the following situation. I have been following the example to create a 3d surface, as explained here:

http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo2.html

and I have altered the code slightly to plot a straight red line from the center outside of the sphere (see code at the end of this email). As one can see, the whole line is visible always, no matter how the sphere is turned. Is there a way to ‘hide’ those parts of the red line, which are ‘behind’ the blue surface? Like you stick a pencil into an apple and turn the apple, so you can see parts of the pencil, depending on how the apple is rotated (except the part of the pencil inside the apple)?

If someone can help me with that problem that would be great.

Thanks

Alex

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

import numpy as np

fig = plt.figure()

ax = Axes3D(fig)

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

ax.plot([0,15],[0.0,0.0],‘r’, lw=4)

ax.plot_surface(x, y, z, rstride=4, cstride=4, color=‘b’)

plt.show()

Mplot3d is not a true 3d plotting system. I would recommend mayavi

for that. Because mplot3d is a hack to render 3d objects with a 2d

rendering system, matplotlib can’t get the the layering right. Maybe

in the far future this will be changed, but for now, it is a design

limitation.

Ben