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?