Search code examples
pythonmatplotlibsurfacemplot3d

Surface plot with wireframe


I want to have a plot_surface with "helplines" on the surface.

My code so far:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
X, Y = np.mgrid[-1:1:30j, -1:1:30j]
Z = np.sin(np.pi*X)*np.sin(np.pi*Y)
ax.plot_surface(X, Y, Z, cmap="autumn_r", rstride=1, cstride=1)
ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1, colors= 'black', linewidth= .1)
plt.show()

However, this gives ugly helplines also where I don't want to see them, i.e., on the back side of the surface:

surfaceplot_with_bad_wireframe

I tried playing with alpha parameters but nothing worked.

Obviously, things get even worse when plotting several surfaces in the same plot.

What's the magic behind making the helplines only visible where I'd see them from the pov in "real life"?

Thanks in advance...


Solution

  • Rather than also plotting a wireframe, you can instead just set the line style for the plot_surface command, e.g.,

    ax.plot_surface(
        X,
        Y,
        Z,
        cmap="autumn_r",
        rstride=1,
        cstride=1,
        edgecolor="black",  # set edgecolours
        linewidth=0.1,      # set line width
    )
    

    This produces:

    enter image description here