0

I want to plot a 3d plot from a mat file. Here is my code:

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

But when I try to run this code i get an error:

> 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'

What should I do with my Zcoor for this to work? The mat_data is in this format:

[[x1 y1 z1]
[x2 y2 z2]
...
[xn yn zn]]
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Matt
  • 556
  • 8
  • 31
  • `plot_surface` requires 2D arrays as input, not 1D lists. – ImportanceOfBeingErnest Jan 18 '18 at 10:21
  • How should I load this data then? Its works fine when i do this: `X,Y = np.meshgrid(Xcoor, Ycoor) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) ax.plot_surface(Xcoor, Ycoor, Z)` – Matt Jan 18 '18 at 10:23
  • It appears strange that `ax.plot_surface(Xcoor, Ycoor, Z)` should work correctly, since that would suggest that you have no doubled coordinates in your matrix (that it is in fact rather a line than a surface). In any case without further knowledge about the data in use here, one may indeed close this as duplicate of the most general case of non, uniform, unordered coordinates. – ImportanceOfBeingErnest Jan 18 '18 at 11:01

0 Answers0