axes3d.plot_wireframe(X,Y,Z) Error
Solution 1
I walked around this problem by doing two things.
- import numpy as np
- making the z-axis a multidimensional array
#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.
Hope it solves your problem
Solution 2
Running your code with either Python 2.7.10 or Python 3.6.0, with matplotlib version 2.0.2, yields the same image with no error:
This is not a wireframe though, and a simple ax.plot(X, Y, Z)
would have generated it. As DavidG and ImportanceOfBeingErnest cleverly mentioned, it makes no sense to pass 1D lists to the wireframe function, as X, Y and Z should be two-dimensional.
The following code (an example taken from the matplotlib official documentation) shows exactly how the parameters of the plot_wireframe
function should be (using numpy
arrays):
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
'''
def get_test_data(delta=0.05):
from matplotlib.mlab import bivariate_normal
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1
X = X * 10
Y = Y * 10
Z = Z * 500
return X, Y, Z
'''
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y, z = axes3d.get_test_data(0.05)
ax.plot_wireframe(x,y,z, rstride=2, cstride=2)
plt.show()
The output image is a true wireframe:
Printing x.shape
, for instance, yields you (120, 120), showing that the array is two-dimensional and have 120 positions in the first dimension and 120 positions in the second one.
Solution 3
The command
ax.plot_wireframe(x,y,z, rstride=2, cstride=2)
is creating the problems with the latest versions.
Try using:
ax.plot(x,y,z)
This will definitely solve your issues. Python has been known for being inconsistent with the older libraries. I am getting this image as the output: This is the 3d Image I am getting
Solution 4
I had the exact problem (example from video not working though exactly copied). Without looking into the source code I'm assuming a reality check was added to matplotlib 2.1.0 that NOW stops 1D arrays from being used in plot_wireframe. Changing that method call to simply "plot" did indeed fix the problem.
Related videos on Youtube
A. Lyoussi
Updated on June 04, 2022Comments
-
A. Lyoussi almost 2 years
I'm trying to learn Python through a tutorial on youtube and I'm having some difficulies working with 3D graphs. Long stories short, I continuously get (if
Z.ndim != 2: AttributeError: 'list' object has no attribute 'ndim')
error while trying to launch this simple program:
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()
I know that it is related to the
Axes3.plot_wireframe()
method but Could anyone explain to me what's happening.-
DavidG over 6 years
-
-
ImportanceOfBeingErnest over 6 yearsThe code from the question should throw an error. This is because giving 1D lists to the wireframe function is not meaningful as can be seen from the produced plot. I guess the fact that it does not produce an error is actually a bug in your matplotlib version.
-
joaofbsm over 6 yearsYou can see that the plotted points are exactly what they should be. If it is a bug, it is a really weird one, that gives exactly what he was looking for.
-
ImportanceOfBeingErnest over 6 yearsNo, this is not a wireframe, this is a simple line. You would be expected to use
ax.plot(X,Y,Z)
to draw such a line. -
joaofbsm over 6 yearsHm, I see. I'm going to change my answer.