将底图上的不规则散点数据网格化,尝试使用不同的分辨率

问题描述 投票:0回答:1

我正在尝试在一定大小的网格中进行插值/插值,将我的数据集的不规则分散位置(纬度)与变量值绑定在一起。我的数据可以作为数据框使用,其中的列分别标记了变量,纬度和经度的值。

我必须首先通过优化网格大小来对数据进行网格化,然后找到最佳方法来对网格框中不同数量的点取平均值。

我通过遵循在线示例尝试了代码。我使用histogram2d函数对纬度和经度进行网格化。我用密度计数(等于网格内所有点的平均值)填充具有分散点的网格框。 (然后,我将不得不使用从散点生成的新网格化数据与另一个具有不同网格分辨率的数据集进行比较)。

理想情况下,它应该可以正常工作,但没有散点的网格框将被填充,而有散点的网格框将被排除。不匹配的分辨率越高,则箱尺寸越小。

我查看了这些示例-example 1example 2

这是我的代码的一部分:

df #Dataframe as a csv file opened in pandas

y = df['lon']
x = df['lat']
z = df['var']

# Bin the data onto a 10x10  grid or into any other size 
# Have to reverse x & y due to row-first indexing
zi, yi, xi = np.histogram2d(y, x, bins=(5,5), weights=z, normed=False)
counts, _, _ = np.histogram2d(y, x, bins=(5,5))

zi = zi / counts
zi = np.ma.masked_invalid(zi)


m = Basemap(llcrnrlat=45,urcrnrlat=55,llcrnrlon=25,urcrnrlon=30) 

m.drawcoastlines(linewidth =0.75, color ="black")
m.drawcountries(linewidth =0.75, color ="black")

m.drawmapboundary()

p,q = m(yi,xi)
#cs=m.pcolormesh(xi, yi, zi, edgecolors='black',cmap = 'jet')
cs=m.pcolormesh(p, q, zi, edgecolors='black',cmap = 'jet')
m.colorbar(cs)

#scat = m.scatter(x,y, c=z, s=200,edgecolors='red')


scat=m.scatter(y,x, latlon=True,c=z, s =80)

以下是正在生成的图像。

任何帮助将不胜感激。

enter image description here

python grid matplotlib-basemap spatial-interpolation
1个回答
0
投票

[一个朋友帮我弄清楚了。

在进行pcolormesh图时,必须转置从直方图生成的数组矩阵:

cs=m.pcolormesh(p, q, zi.T, edgecolors='black',cmap = 'jet')
© www.soinside.com 2019 - 2024. All rights reserved.