Python 使用matplotlib绘制4D numpy数据

Python 使用matplotlib绘制4D numpy数据,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,我有一个三维numpy阵列,其尺寸为1400x1400x29。但是,数据是4D,因为每个x、y、z都有一个不同的值(第四维)。我相信可以像下面这样做 import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D //some calculation that creates a 3D array called "cube" fig = plt.figure() ax =

我有一个三维numpy阵列,其尺寸为1400x1400x29。但是,数据是4D,因为每个x、y、z都有一个不同的值(第四维)。我相信可以像下面这样做

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

//some calculation that creates a 3D array  called "cube"

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for x in range(1400):
    for y in range(1400):
        for z in range(29):
            ax.scatter(x, y, z, c=cube[x,y,z])
plt.show()
但是,上面的脚本给了我一个错误,即 “TypeError:类型为'numpy.float64'的对象没有len()”

编辑1 完整错误消息

File "cube.py", line 57, in <module>
    ax.scatter(x, y, z, c=cube[z , x , y], cmap=plt.hot())
  File "/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site-packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/mpl_toolkits/mplot3d/axes3d.py", line 2353, in scatter
    xs, ys, s=s, c=c, *args, **kwargs)
  File "/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site-packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/matplotlib/__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site-packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/matplotlib/axes/_axes.py", line 4050, in scatter
    colors = mcolors.to_rgba_array(c)
  File "/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site-packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/matplotlib/colors.py", line 231, in to_rgba_array
    result = np.empty((len(c), 4), float)
TypeError: object of type 'numpy.float64' has no len()
文件“cube.py”,第57行,在
ax.scatter(x,y,z,c=cube[z,x,y],cmap=plt.hot())
文件“/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/mpl_toolkits/mplot3d/axes3d.py”,第2353行,分散显示
xs,ys,s=s,c=c,*args,**kwargs)
文件“/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/matplotlib/uu_init_______;.py”,内部第1710行
返回函数(ax,*args,**kwargs)
文件“/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/matplotlib/axes/_axes.py”,第4050行,散点
颜色=mcolors.to_rgba_数组(c)
文件“/pawsey/cle60up05/python/2.7.14/matplotlib/2.1.0/lib/python2.7/site packages/matplotlib-2.1.0-py2.7-linux-x86_64.egg/matplotlib/colors.py”,第231行,在to_rgba_数组中
结果=np.空((len(c),4),浮点)
TypeError:类型为“numpy.float64”的对象没有len()

谢谢

所以理解
立方体
是一个
形状
(1400、1400、29)
的小数组,绘制三维散点的正确方法是:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

//some calculation that creates a 3D array  called "cube"

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = np.mgrid[:1400, :1400, :29]
ax.scatter(X, Y, Z, c=cube.ravel())
plt.show()

您必须使用数组而不是标量调用
ax.scatter
。另外,它采用1D数组作为
c
输入,因此我调用了
ravel()
np.mgrid
只是一种快速创建N维统一网格的方法。它相当于
np.arange
np.meshgrid
。如果您想了解更多,我建议您阅读其中每一项的文档

因此理解
cube
是一个
numpy.ndarray
形状
(1400、1400、29)
,绘制三维散点的正确方法是:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

//some calculation that creates a 3D array  called "cube"

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = np.mgrid[:1400, :1400, :29]
ax.scatter(X, Y, Z, c=cube.ravel())
plt.show()

您必须使用数组而不是标量调用
ax.scatter
。另外,它采用1D数组作为
c
输入,因此我调用了
ravel()
np.mgrid
只是一种快速创建N维统一网格的方法。它相当于
np.arange
np.meshgrid
。如果您想了解更多,我建议您阅读其中每一项的文档

请添加完整的错误消息。@DYZ我已经用完整的错误消息编辑了问题“代码
cube
做什么?”?也可以添加该代码。请添加完整的错误消息。@DYZ我已经用完整的错误消息编辑了问题“代码
cube
做什么?”?也许还可以添加该代码