4

说我有以下内容:

import pyplot as plt
import numpy as np
'''array([[29, 13, 11,  4,  5], #dataMag
       [19, 16, 25,  9, 10],
       [16, 22, 14, 18, 26],
       [ 9, 17,  8,  9, 777]])

array([[205, 338, 380, 428, 228], #dataX
       [199, 546, 430,  95, 374], 
       [418,  85, 260, 236, 241],
       [308, 481, 133, 136,  83]])

array([[ 0.48,  0.83,  0.71,  0.12,  0.],   #dataY
       [ 0.09,  0.  ,  0.7 ,  0.43,  0.54],
       [ 0.58,  0.  ,  0.56,  0.18,  0.25],
       [ 0.96,  0.26,  0.57,  0.  ,  0.82])'''

plt.scatter(x=dataX.flat, y=dataY.flat, c=dataMag.flat, vmin=np.min(dataMag),
            vmax=np.max(dataMag), marker='s', cmap='hot')
plt.show()

这给了我以下结果: 随机生成的图像

Numpy(或Scipy等)中是否有办法将它们表示为(a,b)二维数组,而不是使用三个数组来获取二维图像?

4

1 回答 1

1

编辑我在下面保留我的原始答案,但深入研究您之前关于同一主题的问题,代码执行您所追求的。请注意,它不处理重复值,因此如果您将多个值分配给同一位置,则只会保留其中一个。此外,这会弄乱散点图的比例,所以像我原来的答案这样的东西可能更适合你所追求的。但无论如何,这是代码:

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]])

原始答案

如果dataXdataYwhere 两个整数数组,实现将非常简单。但由于它们似乎不一定是,您需要进行一些舍入,为此您需要首先为每个方向的数组选择一个步长,然后您可以执行以下操作:

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。

于 2013-01-03T23:35:24.280 回答