简体   繁体   中英

TypeError: Input z must be 2D, not 3D

I have the following code:

x = np.arange(0,4)
y = np.arange(6,10)
X, Y = np.meshgrid(x,y)
print(a.shape,x.shape,y.shape)
plt.contour([X,Y,],a)
plt.show()

which gives the following output:

(4, 4) (4,) (4,)
TypeError: Input z must be 2D, not 3D

Clearly, the array a is 2-D as when printing the shape I get (4,4) . Looking at the documentation, it also gives the syntax to be contour([X, Y,] Z, [levels], **kwargs) , which I believe I've followed (except for the comma after [X,Y,] but when I cut that out I get a syntax error so I'm assuming it's a typo but I could be wrong). Putting levels in/taking them out makes no difference.

What am I doing wrong?

Given the shape of your arrays, the problem isn't with the shape of a as you expected, but with the fact that you shouldn't put X and Y in a list when calling plt.contour . I guess you should write :

plt.contour(X, Y, a)

Note that the function signature contour([X, Y,] Z, [levels], **kwargs) mean that X and Y (as well as levels ) are optional parameters (not that they should be put in a list when calling the function). So it is also valid, for example, to call plt.contour like so :

plt.contour(a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM