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?

2
  • 1
    This might help: stackoverflow.com/questions/5957380/… Commented Oct 31, 2013 at 14:16
  • @atomh33ls Thanks. I did search a lot, but didn't find a solution. Probably I should mark my question as duplicate. Commented Oct 31, 2013 at 14:32

1 Answer 1

2

You can use

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

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.