编辑我在下面保留我的原始答案,但深入研究您之前关于同一主题的问题,代码执行您所追求的。请注意,它不处理重复值,因此如果您将多个值分配给同一位置,则只会保留其中一个。此外,这会弄乱散点图的比例,所以像我原来的答案这样的东西可能更适合你所追求的。但无论如何,这是代码:
x_, x_idx = np.unique(np.ravel(dataX), return_inverse=True)
y_, y_idx = np.unique(np.ravel(dataY), return_inverse=True)
newArray = np.zeros((len(x_), len(y_)), dtype=dataMag.dtype)
newArray[x_idx, y_idx] = np.ravel(dataMag)
>>> newArray
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 777, 0, 0],
[ 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0],
[ 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0],
[ 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
原始答案
如果dataX
和dataY
where 两个整数数组,实现将非常简单。但由于它们似乎不一定是,您需要进行一些舍入,为此您需要首先为每个方向的数组选择一个步长,然后您可以执行以下操作:
from __future__ import division
x_step, y_step = 25, 0.10
x = np.round(dataX / x_step).astype(int)
y = np.round(dataY / y_step).astype(int)
x_m, x_M = np.min(x), np.max(x)
y_m, y_M = np.min(y), np.max(y)
newArray = np.zeros((x_M - x_m + 1, y_M - y_m + 1), dtype=dataMag.dtype)
newArray[x - x_m, y - y_m] = dataMag
>>> newArray
array([[ 22, 0, 0, 0, 0, 0, 0, 0, 777, 0, 0],
[ 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0],
[ 9, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 19, 0, 0, 0, 29, 0, 0, 0, 0, 0],
[ 5, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 26, 0, 0, 0, 14, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0],
[ 0, 0, 0, 0, 0, 10, 0, 11, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 4, 0, 0, 0, 0, 16, 25, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
执行此操作时必须小心,确保舍入步长足够小,以免两个值舍入到数组中的同一位置,否则会丢失信息。例如:
x_step, y_step = 50, 0.10
...
>>> newArray
array([[ 22, 0, 0, 0, 9, 0, 0, 0, 777, 0, 0],
[ 9, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0],
[ 0, 19, 0, 0, 0, 29, 0, 0, 0, 0, 0],
[ 5, 0, 26, 0, 0, 0, 14, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9],
[ 0, 0, 0, 0, 0, 10, 0, 0, 13, 0, 0],
[ 0, 0, 0, 0, 0, 0, 16, 11, 0, 0, 0],
[ 0, 4, 0, 0, 0, 0, 0, 25, 0, 0, 0],
[ 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0],
[ 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
并且在位置上[3, 2]
仅显示 26,而不是前面示例中相应单元格中的 18 和 26。