numpy “list”对象没有plot_wireframe的属性“ndim”

rjzwgtxy  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(180)

下面是一个例子:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# Initialize figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Cube data
X = [0, 1, 1, 0, 0, 1, 1, 0]
Y = [0, 0, 1, 1, 0, 0, 1, 1]
Z = [0, 0, 0, 0, 1, 1, 1, 1]
# Plotting cube
ax.plot_wireframe(X, Y, Z)
# Show plot
plt.show()

似乎变量(X, Y, Z)不起作用,并产生以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[53], line 8
      6 Z = [0, 0, 0, 0, 1, 1, 1, 1]
      7 # Plotting cube
----> 8 ax.plot_wireframe(X, Y, Z)
      9 # Show plot
     10 plt.show()

File ~\anaconda3\envs\py11\Lib\site-packages\mpl_toolkits\mplot3d\axes3d.py:1731, in Axes3D.plot_wireframe(self, X, Y, Z, **kwargs)
   1693 """
   1694 Plot a 3D wireframe.
   1695 
   (...)
   1727     Other keyword arguments are forwarded to `.Line3DCollection`.
   1728 """
   1730 had_data = self.has_data()
-> 1731 if Z.ndim != 2:
   1732     raise ValueError("Argument Z must be 2-dimensional.")
   1733 # FIXME: Support masked arrays

AttributeError: 'list' object has no attribute 'ndim'

如何在线框上绘制这三个列表?

q9yhzks0

q9yhzks01#

错误提示z.ndim无效,因为Z是一个列表。但这是测试Z是二维的一部分。
Z作为一个数组,我们得到2d错误消息:

In [46]: Z=np.array(Z)
In [47]: ax.plot_wireframe(X, Y, Z)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[47], line 1
----> 1 ax.plot_wireframe(X, Y, Z)

File ~\miniconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py:1628, in Axes3D.plot_wireframe(self, X, Y, Z, **kwargs)
   1626 had_data = self.has_data()
   1627 if Z.ndim != 2:
-> 1628     raise ValueError("Argument Z must be 2-dimensional.")
   1629 # FIXME: Support masked arrays
   1630 X, Y, Z = np.broadcast_arrays(X, Y, Z)

ValueError: Argument Z must be 2-dimensional.

从线框文档:

Signature: ax.plot_wireframe(X, Y, Z, **kwargs)
Docstring:
Plot a 3D wireframe.
...
Parameters
----------
X, Y, Z : 2D arrays
    Data values.

相关问题