Reputation: 1280
I want to draw a vertical plane defined by
5 = x + y
in a 3D figure, using Matplotlib.
I had a look at this and this, but no chance. I also found mpl_toolkits.mplot3d.art3d.line_2d_to_3d
at this link, which says
Convert a 2D line to 3D
Looked promising to me, but I could not figure out how to use it.
Now, how would you modify the following code to achieve my objective?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
xs = np.linspace(0, 10, 100)
ys = np.linspace(0, 10, 100)
X, Y = np.meshgrid(xs, ys)
Z # ?????????
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()
Thanks for you help in advance.
Upvotes: 7
Views: 7865
Reputation: 12410
Your mistake is that you define xs
and ys
as independent variables, while they are dependent (x + y = 5). zs
is here independent:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
xs = np.linspace(0, 10, 100)
zs = np.linspace(0, 10, 100)
X, Z = np.meshgrid(xs, zs)
Y = 5 - X
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()
Sample output:
Upvotes: 9