matplotlib 3D表面图

时间:2019-03-04 01:13:34

标签: python matplotlib jupyter

尝试使用jupyter笔记本创建平面的3D表面图。但是,这段代码给了我一个奇怪的错误:

# ValueError: Argument Z must be 2-dimensional.

%matplotlib
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
plt.clf()
ax = Axes3D(fig)

x=np.arange(-4,4,1)
y=np.arange(-4,4,1)

X = []
Y = []
Z = []

for ix in range(0, len(x)):
    for iy in range(0, len(y)):
        z = 2*x[ix] + 3*y[iy]
        Z.append(z)
        X.append(x[ix])
        Y.append(y[iy])

ax.plot_surface(X, Y, Z)

plt.show()

1 个答案:

答案 0 :(得分:0)

%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)

def plot():
    fig = plt.figure()
    ax = Axes3D(fig)
    #ax = fig.gca(projection='3d')
    ax.plot_surface(X, Y, Z)
    plt.show()

Z = 2*X + 3*Y
plot()