Skip to content Skip to sidebar Skip to footer

Converting Astropy.table.columns To A Numpy Array

I'd like to plot 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.83203

Solution 1:

An astropy Table Column object can be converted to a numpy array using the data attribute:

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

In [8]: c.data
Out[8]: array([1, 2, 3])

You can also convert an entire table to a numpy structured array with the as_array() Table method (e.g. data.as_array() in your example).

BTW I think the actual problem is not about astropy Column but your numpy array creation statement. It should probably be:

arr = np.array([data['ra'], data['dec']])

This works with Column objects.

Solution 2:

The signature of numpy.array is numpy.array(object, dtype=None,)

Hence, when calling np.array([data['ra']], [data['dec']]), [data['ra']] is the object to convert to a numpy array, and [data['dec']] is the data type, which is not understood (as the error says).

It's not actually clear from the question what you are trying to achieve instead - possibly something like

points = np.array([data['ra'], data['dec']])

Solution 3:

Keep in mind, though, that if you actiually want is to plot points you don't need to convert to arrays. The following will work just fine:

from matplotlib import pyplot as plt
plt.scatter(data['ra'], data['dec'])

With no need to do any conversion to arrays.

Post a Comment for "Converting Astropy.table.columns To A Numpy Array"