You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib调用plot_wireframe时报'list'无ndim属性错误如何解决

错误原因

Matplotlib的3D线框图接口plot_wireframe()要求输入的X、Y、Z三个参数均为二维数组,用于定义三维空间中的网格曲面坐标。你当前传入的是普通一维Python列表,列表本身不存在ndim(数组维度)属性,Matplotlib在校验输入参数维度时直接触发了AttributeError: 'list' object has no attribute 'ndim'报错。

解决方案

根据你的实际需求选择对应修改方案:

方案1:绘制3D空间折线(按顺序连接给定的10组(x,y,z)坐标点)

直接将plot_wireframe方法替换为支持一维坐标输入的plot3D即可,修改后完整代码如下:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import style
style.use('fivethirtyeight')

fig = plt.figure()
ax1 = fig.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 = [1,2,6,3,2,7,3,3,7,2]

ax1.plot3D(x,y,z)
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')

plt.show()

方案2:绘制3D网格线框图

你需要先通过numpy.meshgrid将一维的x、y转换为二维坐标网格,同时将z调整为和网格形状匹配的二维数组,示例代码如下:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style
style.use('fivethirtyeight')

fig = plt.figure()
ax1 = fig.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]
# 生成二维坐标网格
X, Y = np.meshgrid(x, y)
# 可根据实际业务逻辑生成对应二维Z值,此处用二元函数做演示
Z = np.sin(X/2) + np.cos(Y/3)

ax1.plot_wireframe(X,Y,Z)
ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')

plt.show()

内容的提问来源于stack exchange,提问作者Better_By_1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 08:42:02