简体   繁体   中英

Plot 3D surface with Matplotlib a*y + b*x + c

I'm new to Matplotlib and I need to plot a plane in a 3d graph. I have the values for a, b and c in the equation, something like 1y + 2x + 3 .

theta = np.array([1,2,3])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(theta[0],theta[1],theta[2])
plt.show()

I know it's not the right way to use the plot_surface() function but I can't figure out how to.

Update 1

I figured something out using a wireframe.

# Plot the plane
X = np.linspace(0,100, 500)
Y = np.linspace(0,100, 500)
Z = np.dot(theta[0],X) + np.dot(theta[1],Y) + theta[2]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X,Y,Z)
plt.show()

But it only shows a line.

在此处输入图片说明

Try this:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = X + Y * 2 + 3
# Plot the surface.
ax.plot_surface(X, Y, Z, linewidth=0)
plt.show()

You need to first create a meshgrid of your variables, then calculate function values on the meshgrid.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM