[英]matplotlib (mplot3d) - how to increase the size of an axis (stretch) in a 3D Plot?
[英]Matplotlib — mplot3d: triplot projected on z=0 axis in 3d plot?
我正在尝试在两个变量中绘制一个函数,这些变量在一组已知三角形上逐段定义,如下所示:
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。 这是我现在使用的实际实现的简化版本,因此随机散射可能看起来有点怪异。
我不是专家,但这是一个有趣的问题。 经过一番戳后,我想我已经接近了。 我手动创建了Triangulation对象,然后将其和一个零列表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()
预计到达时间:在Poly3DCollection上添加了对set_facecolor的调用,使其变为白色而不是遵循颜色图。 可以用效果来装饰...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.