Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove `dtype` fields from structured array [duplicate]

I am using a package containing a function that returns a list of polygon vertices (let's call it polylist). Each element of this list (that have tens of thousands elements) has the following structure

p0 = polylist[0] = 
array([(137.487773, 65.338638), 
       (138.29366, 64.992167),
       (140.625, 64.992167), 
       ...,
       (140.625, 65.402088), 
       dtype=[('ra', '>f8'), ('dec', '>f8')])

To plot the elements in polylist, I want to use matplotlib.patches.Polygon or matplotlib.collections.PolyCollection.

But both Polygon and PolyCollection need the vertices to be a 2d numpy array of shape (N,2). So I need to convert p0 into something like

p0 = 
array([[137.487773, 65.338638], 
       [138.29366, 64.992167],
       [140.625, 64.992167], 
       ...,
       [140.625, 65.402088]])

If I do np.array(p0, dtype=float) I get only p0['ra']

array([137.487773,  138.29366,  140.625,  140.625, ..., 140.009968])

To ways came to my mind

np.array((v['ra'], v['dec'])).T

or

np.vstack((v['ra'], v['dec'])).T

Is there any better way or any numpy function to do it?

like image 264
Francesco Montesano Avatar asked Dec 20 '25 15:12

Francesco Montesano


1 Answers

You can use

p0.view(('>f8', 2))

See view docs and also http://wiki.scipy.org/Cookbook/Recarray

like image 105
Janne Karila Avatar answered Dec 22 '25 06:12

Janne Karila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!