Surface contour and 3d in matplotlib

I would like to build a surface with a color scheme, wireframe and contours using matplotlib . Something like that:

enter image description here

Please note that I am not asking about contours lying in a plane parallel to xy, but those that are 3D and white in the image.

If I go in a naive way and speak all this, I will not be able to see the outlines (see code and image below).

 import numpy as np from mpl_toolkits.mplot3d import axes3d 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", lw=0.5, rstride=1, cstride=1) ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1) ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid") plt.show() 

enter image description here

If I add transparency to the surface faces, then I can see the contours, but it looks really cluttered (see code and image below)

 import numpy as np from mpl_toolkits.mplot3d import axes3d 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", lw=0.5, rstride=1, cstride=1, alpha=0.5) ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1) ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid") plt.show() 

enter image description here

Question: Is there a way to get this result in matplotlib ? However, no shading is required.

+5
source share
2 answers

Apparently this is a mistake if you try this

 import numpy as np from mpl_toolkits.mplot3d import axes3d 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", lw=0, rstride=1, cstride=1) ax.contour(X, Y, Z+1, 10, lw=3, colors="k", linestyles="solid") plt.show() 

And turn around, you will see that the contour lines disappear when they should not

+2
source

I think you want to set the offset to the path:

 ax.contour(X, Y, Z, 10, offset=-1, lw=3, colors="k", linestyles="solid", alpha=0.5) 

See this example for more details:

http://matplotlib.org/examples/mplot3d/contour3d_demo3.html

And the docs here:

http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#contour-plots

displacement: if the projection graph of the contour lines is set in this position in the plane normal to zdir

Note zdir = 'z' by default, but you can project the zdir setting in the x or y direction, respectively.

0
source

Source: https://habr.com/ru/post/1243184/


All Articles