0

我有以下代码来绘制平面x+y+z=1,但不幸的是,尽管我将所有变量的限制设置为 [0,1],但它仍然在负区域绘制延续。我怎样才能让表面有界x,y,z>0

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator, FormatStrFormatter

point  = np.array([1, 0, 0])
normal = np.array([1, 1, 1])
fig = plt.figure()
ax = fig.gca(projection='3d')

# a plane is a*x+b*y+c*z+d=0
# [a,b,c] is the normal. Thus, we have to calculate
# d and we're set
d = -point.dot(normal)

# create x,y
X = np.arange(0, 1, 0.02)
Y = np.arange(0, 1, 0.02)
X, Y = np.meshgrid(X, Y)

# calculate corresponding z
Z = (-normal[0] * X - normal[1] * Y - d) * 1. /normal[2]
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
        linewidth=0, antialiased=False)

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

plt.show()

自己找一个技巧,但这是hack而不是正确的做法:添加了以下几行:

Y[Z<0]=None
X[Z<0]=None
Z[Z<0]=None
4

0 回答 0