Python 插值后的整形

Python 插值后的整形,python,numpy,grid,scipy,reshape,Python,Numpy,Grid,Scipy,Reshape,将数据插值到目标网格后,我无法重塑数据以匹配原始形状。我的数据的原始形状是900x900,是行x列。插值后,我有一个一维插值数组,目标网格的新大小为2506。为了进一步处理,我需要2506x2506成型 以下是我的情况: #SOURCE GRID + DATA xs = [ 3.58892995, 3.60107571, 3.61322204, ..., 15.67397575, 15.68906607, 15.70415559] ys = [ 46.95258041,

将数据插值到目标网格后,我无法重塑数据以匹配原始形状。我的数据的原始形状是900x900,是行x列。插值后,我有一个一维插值数组,目标网格的新大小为2506。为了进一步处理,我需要2506x2506成型

以下是我的情况:

#SOURCE GRID + DATA
xs = [  3.58892995,   3.60107571,   3.61322204, ...,  15.67397575,
    15.68906607,  15.70415559]
ys = [ 46.95258041,  46.95351109,  46.95444002, ...,  54.7344427 ,
    54.7335759 ,  54.7327068 ]
# data.shape => (900,900), e.g. (rows, columns)
data = [[-- 0.43 -- ..., -- -- --]
       [-- -- -- ..., -- 0.21 --]
       [-- -- -- ..., -- -- --]
        ..., 
       [-- 1 -- ..., -- -- --]
       [-- 0.12 -- ..., -- -- --]
       [-- -- -- ..., -- -- --]]

values = data.flatten()

#TARGET GRID
xt = np.linspace(2, 9, 2506)
yt = np.linspace(44, 52, 2506)

#INTERPOLATION
Z = griddata((xs, ys), values, (xt, yt), method='nearest')
#Z = [-- -- -- ..., 0.0 0.0 0.0]
#Z.shape -> (2506,) BUT i need it in (2506, 2506)
#Z = np.reshape(Z, (2506, 2506)) is not working ofc

我不确定使用mgrid、meshgrid或重塑是否是解决此问题的正确方法。谢谢你的帮助

matplotlib中的网格数据可能更符合您的要求。您可以执行以下操作:

from numpy.random import uniform, seed
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy as np

datatype = 'grid'

npts = 900
xs = uniform(-2, 2, npts)
ys = uniform(-2, 2, npts)

# define output grid.
xt = np.linspace(-2.1, 2.1, 2506)
yt = np.linspace(-2.1, 2.1, 2506)

#Input is a series of 900 points
if datatype is 'points':
    values = xs*np.exp(-xs**2 - ys**2)
    # grid the data.
    Z = griddata(xs, ys, values, xt, yt, interp='nn')

#Input is a 900 x 900 grid of x,y,z points 
elif datatype is 'grid':

    X, Y = np.meshgrid(xs,ys, indexing='ij')
    values = 2 - 2 * np.cos(X)*np.cos(Y) - np.cos(X - 2*Y)

    # grid the data.
    Z = griddata(X.ravel(), Y.ravel(), values.ravel(), xt, yt, interp='nn')


# contour the gridded data, plotting dots at the nonuniform data points.
CS = plt.contour(xt, yt, Z, 15, linewidths=0.5, colors='k')
CS = plt.contourf(xt, yt, Z, 15, cmap=plt.cm.RdYlBu_r)
plt.colorbar()

# plot data points.
plt.scatter(xs, ys, marker='o', c='b', s=5, zorder=10)
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.show()
其中Z的尺寸为2506x2506(根据需要)。输入数据可以是一系列点(900 x位置、900 y位置和这些位置的900个值)或网格。我添加了一个数据类型变量以在它们之间进行更改。看起来您的输入数据是网格形式的

用法是

xxs, yys = np.meshgrid(xs, ys, indexing='ij')
Z = griddata((xxs.ravel(), yys.ravel()), values,
             (xt[:,np.newaxis], yt[np.newaxis,:]),
             method='nearest')
您需要首先从
xs
ys
数组中过滤屏蔽值