将 astropy.table.columns 转换为 numpy 数组

npr*_*oss 2 python arrays types numpy

我想绘制points

points = np.random.multivariate_normal(mean=(0,0), cov=[[0.4,9],[9,10]],size=int(1e4))

print(points)
  [[-2.50584156  2.77190372]
   [ 2.68192136 -3.83203819]
   ..., 
   [-1.10738221 -1.72058301]
   [ 3.75168017  5.6905342 ]]

print(type(points))
  <class 'numpy.ndarray'>

data = ascii.read(datafile)  
type(data['ra'])
  astropy.table.column.Column
type(data['dec'])
 astropy.table.column.Column
Run Code Online (Sandbox Code Playgroud)

然后我尝试:

points = np.array([data['ra']], [data['dec']])
Run Code Online (Sandbox Code Playgroud)

并得到一个

TypeError: data type not understood
Run Code Online (Sandbox Code Playgroud)

想法?

Tom*_*oft 5

可以使用以下属性将 astropy Table Column 对象转换为 numpy 数组data

In [7]: c = Column([1, 2, 3])

In [8]: c.data
Out[8]: array([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)

您还可以使用 Table 方法将整个表转换为 numpy 结构化数组as_array()(例如data.as_array()在您的示例中)。

顺便说一句,我认为实际的问题不是关于 astropyColumn而是你的 numpy 数组创建语句。大概应该是:

arr = np.array([data['ra'], data['dec']])
Run Code Online (Sandbox Code Playgroud)

这适用于Column对象。