Surface and 3d contour in matplotlib

纵然是瞬间 提交于 2019-12-03 17:13:30

问题


I would like to plot a surface with a colormap, wireframe and contours using matplotlib. Something like this:

Notice that I am not asking about the contours that lie in the plane parallel to xy but the ones that are 3D and white in the image.

If I go the naïve way and plot all these things I cannot see the contours (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()

If a add transparency to the surface facets 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()

Question: Is there a way to obtain this result in matplotlib? The shading is not necessary, though.


回答1:


Apparently it is a bug, 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 rotate around, you will see the contour lines disappearing when they shouldn't




回答2:


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

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

See this example for more:

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

And the docs here:

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

offset: If specified plot a projection of the contour lines on this position in plane normal to zdir

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



来源:https://stackoverflow.com/questions/35445424/surface-and-3d-contour-in-matplotlib

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