I have the following code to draw the plane x+y+z=1
, but unfortunately although I set the limits to [0,1] for all the variables it still draws the continuation in the negative area. How can I only get the surface bounded to x,y,z>0
area?
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()
Find a trick myself but this is hack rather than a correct way of doing it: Added up the following lines:
Y[Z<0]=None
X[Z<0]=None
Z[Z<0]=None