我有以下代码来绘制平面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()
自己找一个技巧,但这是黑客而不是正确的方法:添加以下几行:
Y[Z<0]=None
X[Z<0]=None
Z[Z<0]=None