简体   繁体   English

Python:将 Astropy 表转换为数组(使用 Numpy?)进行绘图(在 matplotlib 上)

[英]Python: Converting Astropy Table into an Array (Using Numpy?) for Plotting (on matplotlib)

I have an Astropy Table as such:我有一个这样的 Astropy 表:

         a           b    c 
------------------- ---- ---
-0.6096212422044334  2.0 3.0
-1.2192424844088667 10.0 3.0
   -5.4865911798399  9.0 3.0

I want to turn this table back into an array for plotting purposes.我想将此表转回数组以进行绘图。 This is what I tried:这是我尝试过的:

d=Table(t)
x=np.array(d)
print(x)

This is what I get back (which I believe is a tuple):这就是我得到的(我相信这是一个元组):

[(-0.60962124,  2., 3.) (-1.21924248, 10., 3.) (-5.48659118,  9., 3.)]

When I ask for 'np.shape(x)' I get (3,) which is why I believe it is a tuple.当我要求 'np.shape(x)' 我得到 (3,) 这就是为什么我相信它是一个元组。 I need the shape to be (3,3) so I can call out individual elements and plot this information.我需要形状为 (3,3) 以便我可以调用单个元素和 plot 此信息。

Thank you, Q谢谢你,Q

This is a numpy structured array in which the "elements" of the array are not single float values, but rather triplets (in this case) of floats.这是一个 numpy结构化数组,其中数组的“元素”不是单个浮点值,而是浮点数的三元组(在这种情况下)。 It does this in part because in the general case that all columns are not the same data type, the original format of the data is still preserved (3 columns and 3 (possibly) heterogeneous rows).这样做的部分原因是在所有列的数据类型不同的一般情况下,仍保留数据的原始格式(3 列和 3 个(可能)异构行)。 This is why the shape is (3,) .这就是形状为(3,)的原因。

In this case you can safely convert to a (3, 3) homogenous array because all the columns have a floating point data type.在这种情况下,您可以安全地转换为(3, 3)同构数组,因为所有列都具有浮点数据类型。 There are a few different ways to do this but one of the easiest and safest is the structured_to_unstructured utility function.有几种不同的方法可以做到这一点,但最简单和最安全的方法之一是structured_to_unstructured实用程序 function。

Update: Now that at I'm at my computer, here's a concrete example based on yours:更新:现在我在我的电脑上,这是一个基于你的具体示例:

>>> f = io.BytesIO(b"""\ 
...          a           b    c  
... ------------------- ---- --- 
... -0.6096212422044334  2.0 3.0 
... -1.2192424844088667 10.0 3.0 
...    -5.4865911798399  9.0 3.0 
... """)                                                                                       
>>> t = Table.read(f, format='ascii.fixed_width_two_line')                                     
>>> t                                                                                          
<Table length=3>
         a             b       c   
      float64       float64 float64
------------------- ------- -------
-0.6096212422044334     2.0     3.0
-1.2192424844088667    10.0     3.0
   -5.4865911798399     9.0     3.0
>>> t.as_array()                                                                               
array([(-0.60962124,  2., 3.), (-1.21924248, 10., 3.),
       (-5.48659118,  9., 3.)],
      dtype=[('a', '<f8'), ('b', '<f8'), ('c', '<f8')])
>>> a = np.lib.recfunctions.structured_to_unstructured(t.as_array())                           
>>> a                                                                                          
array([[-0.60962124,  2.        ,  3.        ],
       [-1.21924248, 10.        ,  3.        ],
       [-5.48659118,  9.        ,  3.        ]])
>>> a.shape                                                                                    
(3, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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