使用plot_surface从mat文件中绘制数据

时间:2018-01-18 10:19:37

标签: python matplotlib plot mat

我想从mat文件中绘制一个3d图。这是我的代码:

import scipy.io
import matplotlib.pyplot as plt

mat=scipy.io.loadmat('data.mat')

mat_data = mat['data']
print(mat_data)

Xcoor = []
Ycoor = []
Zcoor = []


for x in mat_data:
    Xcoor.append(x[0])
    Ycoor.append(x[1])
    Zcoor.append(x[2])

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot_surface(Xcoor, Ycoor, Zcoor)

plt.show()

但是当我尝试运行此代码时出现错误:

> Traceback (most recent call last):   File
> "D:/PycharmProjects/Munpy/fromMat.py", line 44, in <module>
>     ax.plot_surface(wspX, wspY, wspZ)   File "C:\Program Files\Python36\Lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line
> 1609, in plot_surface
>     if Z.ndim != 2: AttributeError: 'list' object has no attribute 'ndim'

我应该如何处理Zcoor才能使用? mat_data采用以下格式:

[[x1 y1 z1]
[x2 y2 z2]
...
[xn yn zn]]

0 个答案:

没有答案