【发布时间】: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