3

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?

Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64
  • 1
    This might help: http://stackoverflow.com/questions/5957380/convert-structured-array-to-regular-numpy-array – Lee Oct 31 '13 at 14:16
  • @atomh33ls Thanks. I did search a lot, but didn't find a solution. Probably I should mark my question as duplicate. – Francesco Montesano Oct 31 '13 at 14:32

1 Answers1

2

You can use

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

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

Janne Karila
  • 24,266
  • 6
  • 53
  • 94