matplotlib中的曲面和3d轮廓 [英] Surface and 3d contour in matplotlib

查看:84
本文介绍了matplotlib中的曲面和3d轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用matplotlib绘制带有颜色图,线框和轮廓的表面.像这样:

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

请注意,我并不是在询问与xy平行的平面中的轮廓,而是图像中的 3D和白色轮廓.

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

问题:是否有办法在matplotlib中获得此结果?不过,阴影不是必需的.

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

推荐答案

如果您尝试这样做,显然是一个错误

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

这篇关于matplotlib中的曲面和3d轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆