如何调用二维网格(2D bin)中坐标对应的值?
How to call the value corresponding to a coordinate in a 2D mesh (2D bin)?
我有一个坐标对应的温度。在固定区域上,我想包括一个正方形网格(所有网格的长度必须相同)。为此,我使用 numpy.meshgrid
在整个区域生成单元格。现在我的问题是如何将坐标在第 k 个单元格中的每一行的温度相加?我有点困惑,我应该使用 numpy.histogram2d
吗?它给了我 X
和 Y
的频率,这是否意味着我必须使用多维直方图?
非常感谢!
import pandas as pd
import numpy as np
####generating input data frame
df = pd.DataFrame(data = np.random.randint(2000, 6000, (1000000, 3)))
df.columns= ['X','Y', 'Temp']
x2 = np.linspace(df['X'].min(),df['X'].max(), 20)
y2 = np.linspace(df['Y'].min(),df['Y'].max(), 20 )
xx, yy = np.meshgrid(x2, y2, indexing ='ij')
plt.scatter(xx, yy, color="red", marker="x");
#### Or should I use
Hist, xedges, yedges = np.histogram2d(df['X'], df['Y'], bins = (x2,y2))
H = Hist.T
这会获取您的数据集,并生成一个 20x20 数组,其中包含该网格内所有点的平均温度。如果网格中没有临时工,它将产生 NaN:
import numpy as np
data = np.random.randint(2000, 6000, (100000, 3))
# We divide the coordinate space up into 20 bins.
binsize = (6000-2000) // 20
bins = np.zeros((20,20))
counts = np.zeros((20,20))
for row in data:
binx = (row[0] - 2000) // binsize
biny = (row[1] - 2000) // binsize
bins[biny,binx] += row[2]
counts[biny,binx] += 1
print( bins )
print( counts )
print( "Averages:" )
print( bins / counts )
我有一个坐标对应的温度。在固定区域上,我想包括一个正方形网格(所有网格的长度必须相同)。为此,我使用 numpy.meshgrid
在整个区域生成单元格。现在我的问题是如何将坐标在第 k 个单元格中的每一行的温度相加?我有点困惑,我应该使用 numpy.histogram2d
吗?它给了我 X
和 Y
的频率,这是否意味着我必须使用多维直方图?
非常感谢!
import pandas as pd
import numpy as np
####generating input data frame
df = pd.DataFrame(data = np.random.randint(2000, 6000, (1000000, 3)))
df.columns= ['X','Y', 'Temp']
x2 = np.linspace(df['X'].min(),df['X'].max(), 20)
y2 = np.linspace(df['Y'].min(),df['Y'].max(), 20 )
xx, yy = np.meshgrid(x2, y2, indexing ='ij')
plt.scatter(xx, yy, color="red", marker="x");
#### Or should I use
Hist, xedges, yedges = np.histogram2d(df['X'], df['Y'], bins = (x2,y2))
H = Hist.T
这会获取您的数据集,并生成一个 20x20 数组,其中包含该网格内所有点的平均温度。如果网格中没有临时工,它将产生 NaN:
import numpy as np
data = np.random.randint(2000, 6000, (100000, 3))
# We divide the coordinate space up into 20 bins.
binsize = (6000-2000) // 20
bins = np.zeros((20,20))
counts = np.zeros((20,20))
for row in data:
binx = (row[0] - 2000) // binsize
biny = (row[1] - 2000) // binsize
bins[biny,binx] += row[2]
counts[biny,binx] += 1
print( bins )
print( counts )
print( "Averages:" )
print( bins / counts )