2

多かれ少なかれ次のように、既知の三角形のセットで区分的に定義された 2 つの変数で関数をプロットしようとしています。

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 に投影することで、両方のサブプロットを 1 つに結合します。これは、2 次元プロットの他のバリアントでは可能ですが、トリプロットでは不可能であることはわかっています。私が欲しいものを手に入れることは可能ですか?

PS。これは、私が現在使用している実際の実装の大幅に単純化されたバージョンであるため、ランダムな散乱は少し奇妙に見えるかもしれません。

4

1 に答える 1

0

私は専門家ではありませんが、これは興味深い問題でした。いろいろ調べてみたら、近いものができたと思います。Triangulation オブジェクトを手動で作成し、それと az ゼロのリストを 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 の呼び出しを追加して、カラーマップに従うのではなく、白にするようにしました。目的の効果を得るために futzed することができます...

于 2014-06-22T01:12:57.597 に答える