我正在尝试通过 youtube 上的教程学习 Python,但在处理 3D 图形时遇到了一些困难。长话短说,我不断得到(如果
Z.ndim != 2:
AttributeError: 'list' object has no attribute 'ndim')
尝试启动这个简单程序时出错:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
chart = fig.add_subplot(1,1,1,projection = '3d')
X,Y,Z = [1,2,3,4,5,6,7,8],[2,5,3,8,9,5,6,1],[3,6,2,7,5,4,5,6]
chart.plot_wireframe(X,Y,Z)
plt.show()
我知道它与 Axes3.plot_wireframe()
方法有关,但谁能向我解释发生了什么。
最佳答案
我通过做两件事来解决这个问题。
- 将 numpy 导入为 np
- 将z轴设为多维数组
#My 3d graph
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
figure = plt.figure()
axis = figure.add_subplot(111, projection = '3d')
x = [1,2,3,4,5,6,7,8,9,10]
y = [5,6,7,8,2,5,6,3,7,2]
z = np.array([[1,2,6,3,2,7,3,3,7,2],[1,2,6,3,2,7,3,3,7,2]])
axis.plot_wireframe(x, y, z)
axis.set_xlabel('x-axis')
axis.set_ylabel('y-axis')
axis.set_zlabel('z-axis')
plt.show()
Take special note of the z variable. If z is not multidimensional, it will throw an error.
希望它能解决您的问题
关于python - axes3d.plot_wireframe(X,Y,Z) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47225830/