Numpy:将散点图转换为2D数组

时间:2013-01-03 21:27:15

标签: python numpy matplotlib scipy

说我有以下内容:

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

给了我以下结果: random generated image

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

1 个答案:

答案 0 :(得分: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]])

原始回答

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

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。