我试图用两个变量绘制一个函数,分段定义在一组已知的三角形上,大致如下:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import random
def f( x, y):
if x + y < 1: return 0
else: return 1
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
tris = [[0, 1, 3], [1, 2,3]]
fig = plt.figure()
ax = fig.add_subplot( 121)
ax.triplot( x, y, tris)
xs = [random.random() for _ in range( 100)]
ys = [random.random() for _ in range( 100)]
zs = [f(xs[i], ys[i]) for i in range( 100)]
ax2 = fig.add_subplot( 122, projection='3d')
ax2.scatter( xs, ys, zs)
plt.show()理想情况下,通过将三角形投影到轴z=0,将这两个子图组合成一个。我知道这在2d情节的其他变体中是可能的,但对triplot来说却是不可能的。有可能得到我想要的吗?
PS。这是我现在使用的实际实现的一个非常简化的版本,因此随机分散可能看起来有点奇怪。
发布于 2014-06-22 01:12:57
我不是专家,但这是个有趣的问题。在四处闲逛之后,我想我得到了一些接近的东西。我手动创建了三角剖分对象,然后将它和z个零列表传递到plot_trisurf中,它将三角形放在z=0上的正确位置。
import matplotlib.pyplot as plt
import matplotlib.tri as tri
from mpl_toolkits.mplot3d import Axes3D
import random
def f( x, y):
if x + y < 1: return 0
else: return 1
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
tris = [[0, 1, 3], [1, 2,3]]
z = [0] * 4
triv = tri.Triangulation(x, y, tris)
fig = plt.figure()
ax = fig.add_subplot( 111, projection='3d')
trip = ax.plot_trisurf( triv, z )
trip.set_facecolor('white')
xs = [random.random() for _ in range( 100)]
ys = [random.random() for _ in range( 100)]
zs = [f(xs[i], ys[i]) for i in range( 100)]
ax.scatter( xs, ys, zs)
plt.show()ETA:在set_facecolor上添加了一个Poly3DCollection调用,使其变为白色,而不是遵循颜色图。为了达到预期效果.
https://stackoverflow.com/questions/24335419
复制相似问题