2017-10-04 90 views
1

我是Matplotlib的新手,我需要在三維圖中繪製一個平面。我有等式中的a,b和c的值,例如1y + 2x + 3使用Matplotlib繪製3D曲面a * y + b * x + c

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() 

我知道這是不使用plot_surface()功能以正確的方式,但我無法弄清楚如何。

更新1

我想出了使用線框的東西。

# 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() 

但它只顯示一行。

enter image description here

+1

你的函數只有一個變量,你怎麼能畫從一個3D圖形? –

+0

我犯了一個錯誤,它會是y + 2x + 3 – Servietsky

回答

1

試試這個:

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() 

您需要先創建變量的meshgrid,然後在meshgrid計算函數值。

enter image description here