.plot_wireframes(x,y,z) causing errors
Question:
I’m following a tutorial on how to do 3d Plot graphs in Python and it works fine up until the second last line
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as pt
fig = pt.figure()
chart = fig.add_subplot(1,1,1,projection = "3d")
X,Y,Z = [3,15,23],[7,9,23],[7,10,19]
chart.plot_wireframe(X,Y,Z)
pt.show()
it keeps shooting out this error
Traceback (most recent call last):
File "~~~3D Plot.py", line 6, in <module>
chart.plot_wireframe(X,Y,Z)
File "~~~Python3.11.1Libsite-packagesmpl_toolkitsmplot3daxes3d.py", line 1731, in plot_wireframe
if Z.ndim != 2:
AttributeError: 'list' object has no attribute 'ndim'
/~~~ is just me censoring my file layout
I think it might be an out of date package but I don’t know how to check/update for it. It also mentions ndim which every google search brings me to numpy, which I tried importing but to no success.
When I remove the second last line the code runs and shows an empty graph so I know it’s the second last line breaking something
Answers:
Two issues: X
, Y
, and Z
are Python lists, not NumPy arrays; Z
is one-dimensional. The plot_wireframe
method expects NumPy arrays as inputs, which have the ndim
attribute. To fix the issues, you can convert the lists to NumPy arrays and make Z
two-dimensional:
X, Y, Z = [3, 15, 23], [7, 9, 23], [[7, 10, 19],[7, 10, 19]]
X = np.array(X)
Y = np.array(Y)
Z = np.array(Z)
chart.plot_wireframe(X, Y, Z)
I’m following a tutorial on how to do 3d Plot graphs in Python and it works fine up until the second last line
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as pt
fig = pt.figure()
chart = fig.add_subplot(1,1,1,projection = "3d")
X,Y,Z = [3,15,23],[7,9,23],[7,10,19]
chart.plot_wireframe(X,Y,Z)
pt.show()
it keeps shooting out this error
Traceback (most recent call last):
File "~~~3D Plot.py", line 6, in <module>
chart.plot_wireframe(X,Y,Z)
File "~~~Python3.11.1Libsite-packagesmpl_toolkitsmplot3daxes3d.py", line 1731, in plot_wireframe
if Z.ndim != 2:
AttributeError: 'list' object has no attribute 'ndim'
/~~~ is just me censoring my file layout
I think it might be an out of date package but I don’t know how to check/update for it. It also mentions ndim which every google search brings me to numpy, which I tried importing but to no success.
When I remove the second last line the code runs and shows an empty graph so I know it’s the second last line breaking something
Two issues: X
, Y
, and Z
are Python lists, not NumPy arrays; Z
is one-dimensional. The plot_wireframe
method expects NumPy arrays as inputs, which have the ndim
attribute. To fix the issues, you can convert the lists to NumPy arrays and make Z
two-dimensional:
X, Y, Z = [3, 15, 23], [7, 9, 23], [[7, 10, 19],[7, 10, 19]]
X = np.array(X)
Y = np.array(Y)
Z = np.array(Z)
chart.plot_wireframe(X, Y, Z)