【问题标题】:Insert matplotlib's Path into Numpy ndarray将 matplotlib 的路径插入 Numpy ndarray
【发布时间】:2016-10-21 14:44:51
【问题描述】:

我需要在一个numpy数组中插入一个matplotlib Path对象,应该使用什么dtype?

这是我所拥有的:

import numpy as np

dtypes = np.dtype([('Shape', '<f8', (2,)), ('FIELD2', '<U254'), ('FIELD3', '<U254'),
                   ('FIELD4', '<U254'), ('FIELD5', '<i4'),
                   ('Length', '<f8'), ('OID@', '<i4')])

b = np.array([([ 93.59900552,  22.62355019], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59901266,  22.6233646 ], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59901623,  22.62300054], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59913044,  22.62273999], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59924109,  22.62261507], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59925536,  22.62240805], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59936601,  22.62212966], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59954804,  22.6220083 ], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.59976219,  22.62173348], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1),
 ([ 93.60013339,  22.62131588], u'randomtext', u'atext', 9999, 1, 1.2119301339479824, 1)],
             dtype=dtypes)

我想将点转换为 matplotlib 路径对象,但是当我将 dtypes 设置为:

dtypes = np.dtype([('Shape', object), ('FIELD2', '<U254'), ('FIELD3', '<U254'),
                   ('FIELD4', '<U254'), ('FIELD5', '<i4'),
                   ('Length', '<f8'), ('OID@', '<i4')])

然后转换matplotlib.path.Path() 执行以下操作:

new_array = np.array([], dtypes)
for id in set(b['OID@'].tolist()):
    sub_array = array[np.where(array['OID@'] == oid)]
    geom = matplotlib.path.Path(sub_array['Shape'])
    row = list(sub_array[0])
    row[0] = geom
    new_array = np.array([row], dtypes)
    new_arrray = numpy.vstack([sub_array, new_array]) 

谢谢

【问题讨论】:

  • 为什么投反对票?这是一个简单的问题。
  • 这就是问题所在——问题太简单了。 insert 本身就是模棱两可的。数组是否已经存在,或者您是否试图使数组可以包含这些对象。 ETC? stackoverflow.com/help/mcve
  • @hpaulj - 添加了更多信息,投反对票。
  • 我没有对你的问题投票,但希望我的回答能说明最初的简单问题是多么不充分。即使您进行了编辑,也很难弄清楚您要做什么。

标签: python numpy matplotlib


【解决方案1】:

在对您的编辑感到困惑并进行试验之后,我认为这是怎么回事:

有了你的dtypes,我可以创建一个“空”数组

In [995]: dtypes
Out[995]: dtype([('Shape', 'O'), ('FIELD2', '<U254'), ('FIELD3', '<U254'), ('FIELD4', '<U254'), ('FIELD5', '<i4'), ('Length', '<f8'), ('OID@', '<i4')])
In [996]: x=np.empty((3,),dtypes)
In [997]: x
Out[997]: 
array([(None, '', '', '', 0, 0.0, 0), (None, '', '', '', 0, 0.0, 0),
       (None, '', '', '', 0, 0.0, 0)], 
      dtype=[('Shape', 'O'), ('FIELD2', '<U254'), ('FIELD3', '<U254'), ('FIELD4', '<U254'), ('FIELD5', '<i4'), ('Length', '<f8'), ('OID@', '<i4')])

np.array([],dtypes)np.empty((0,),dtypes) 相同;此 dtype 的零元素数组。这只适用于重复的stacking

我可以使用以下方法创建Path 对象:

In [1000]: apath=matplotlib.path.Path(np.arange(4).reshape(2,2))

Shape 字段有dtype=object;所以我可以为它分配任何对象

In [1001]: x['Shape']
Out[1001]: array([None, None, None], dtype=object)

In [1002]: x['Shape'][0]=apath
In [1003]: x['Shape'][1]=matplotlib.path.Path(np.arange(6).reshape(3,2))
In [1004]: x
Out[1004]: 
array([ (Path(array([[ 0.,  1.],
       [ 2.,  3.]]), None), '', '', '', 0, 0.0, 0),
       (Path(array([[ 0.,  1.],
       [ 2.,  3.],
       [ 4.,  5.]]), None), '', '', '', 0, 0.0, 0),
       (None, '', '', '', 0, 0.0, 0)], 
      dtype=[('Shape', 'O'), ('FIELD2', '<U254'), ('FIELD3', '<U254'), ('FIELD4', '<U254'), ('FIELD5', '<i4'), ('Length', '<f8'), ('OID@', '<i4')])

或者对于单元素数组:

In [1010]: y=np.empty((1,),dtypes)
In [1011]: y['Shape']=apath

也许这更接近你正在尝试的:

使用 dtypes 数组的元素作为“模板”:

In [1012]: x[2]
Out[1012]: (None, '', '', '', 0, 0.0, 0)
In [1013]: row=x[2]
In [1014]: row[0]=apath     # assign `apath` to a slot
In [1015]: row
Out[1015]: 
(Path(array([[ 0.,  1.],
       [ 2.,  3.]]), None), '', '', '', 0, 0.0, 0)

rownp.void 对象,而不是列表或元组

我可以创建一个包含这个对象的数组:

In [1016]: np.array(row,dtypes)
Out[1016]: 
array((Path(array([[ 0.,  1.],
       [ 2.,  3.]]), None), '', '', '', 0, 0.0, 0), 
      dtype=[('Shape', 'O'), ('FIELD2', '<U254'), ('FIELD3', '<U254'), ('FIELD4', '<U254'), ('FIELD5', '<i4'), ('Length', '<f8'), ('OID@', '<i4')])

但是[row] 不起作用

In [1017]: np.array([row],dtypes)
...
ValueError: Setting void-array with object members using buffer.

np.void 变成一个元组确实有效。一般来说,结构化数组是创建或填充一个元组列表(或逐个字段)。

In [1018]: np.array([tuple(row)],dtypes)
Out[1018]: 
array([ (Path(array([[ 0.,  1.],
       [ 2.,  3.]]), None), '', '', '', 0, 0.0, 0)], 
      dtype=[('Shape', 'O'), ('FIELD2', '<U254'), ('FIELD3', '<U254'), ('FIELD4', '<U254'), ('FIELD5', '<i4'), ('Length', '<f8'), ('OID@', '<i4')])

rowtuple(row) 显示相同,但​​显然出于此目的它们并不相同。

所以如果我没看错的话,这真的不是Path 对象问题;甚至没有dtype=object 问题。这是关于创建结构化数组。

【讨论】:

  • 正确,问题是尝试使用指定的 dtypes 创建结构化数组。
【解决方案2】:

如果你想要一个python对象的numpy数组,你可以使用

np.empty(shapeInformation, dtype = object)

然后用你喜欢的任何pythonobject填充数组

【讨论】:

  • 当我使用 ndtypes 将列设置为对象时,它失败了:使用缓冲区设置带有对象成员的 void-array。 dtype([('Geometry', 'O'), ('DEFICIENCY', '
  • np.empty((2,2), dtype = object) 在我的机器上工作正常。请提供一些示例代码,然后我可以进一步帮助您。
猜你喜欢
  • 1970-01-01
  • 2020-04-21
  • 2014-06-19
  • 2018-10-10
  • 2018-04-12
  • 1970-01-01
  • 2018-10-28
  • 2019-12-09
  • 2011-05-02
相关资源
最近更新 更多