【问题标题】:Matplotlib -- mplot3d: triplot projected on z=0 axis in 3d plot?Matplotlib - mplot3d:在 3d 图中投影在 z=0 轴上的三图?
【发布时间】:2014-08-11 16:37:25
【问题描述】:

我试图在两个变量中绘制一个函数,在一组已知三角形上分段定义,或多或少像这样:

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。这是我现在使用的实际实现的一个高度简化的版本,因此随机散射可能看起来有点奇怪。

【问题讨论】:

    标签: python matplotlib plot triangulation mplot3d


    【解决方案1】:

    我不是专家,但这是一个有趣的问题。在做了一些探索之后,我想我得到了一些接近。我手动创建了三角剖分对象,然后将它和一个 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:在 Poly3DCollection 上添加了对 set_facecolor 的调用,使其变为白色,而不是遵循颜色图。可以使用以获得所需的效果......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2021-07-22
      • 2012-01-06
      • 2023-04-06
      • 2011-06-03
      相关资源
      最近更新 更多