问题描述
我在使用 scipy.interpolate.griddata 时遇到错误.我的目标是使用 matplotlib 准备用于绘制轮廓的数据.我已经读到执行此操作的最佳方法是在传递给 griddata 之前使用 linspace 将 x any y 分隔为一维数组.
I am running into an error when using scipy.interpolate.griddata. My goal is to prepare data for contouring using matplotlib. I have read that the best way to perform this is to separate the x any y as 1D arrays using linspace before passing to griddata.
我的 x 和 y 值的最小值和最大值用于输入到 linspace,以便为 GIS 制图目的保持坐标相同(不确定这是否需要在与网格坐标相同的 xy 区域,但我正在以任何方式这样做)
The min and max values of my x and y values are used to input into the linspace, so as to keep the co-ordinates the same for GIS mapping purposes (not sure if this is necessary to have the data points in the same xy area as the grid co-ordinates, but am doing so any way)
Watertable CSV 文件作为具有 x、y 和 z 值的 numpy 数组导入.z 作为直数组列索引提供给 griddata.
The file Watertable CSV is imported as a numpy array with x,y and z values. The z is supplied to griddata as a straight array column index.
我遇到错误valueError:输入数据点的形状无效"
I am running into the error "valueError: invalid shape for input data points"
我相信这是一件非常简单的事情,希望有人能解释我的错误.
I am sure it is something very simple and hopefully someone can shed light on my error.
我已按照建议使用 pastebin 链接了 csv 文件:
I have linked the csv file using pastebin as suggested:
import numpy as np
from scipy.interpolate import griddata
from numpy import genfromtxt
my_data = genfromtxt('WaterTable.csv', delimiter=',')
x = my_data[1:,0:1]
y = my_data[1:,1:2]
z = my_data[1:,2:3]
xmax = max(x)
xmin = min(x)
ymax = max(y)
ymin = min(y)
xi = np.linspace(xmin, xmax, 2000)
yi = np.linspace(ymin, ymax, 2000)
zi = griddata((x, y), z, (xi, yi), method='cubic')
然后我脚本退出并出现以下错误:
I script then exits with the following error:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/GISdev/Irregular_Grid03.py", line 60, in <module>
zi = griddata((x, y), z, (xi, yi), method='cubic')
File "C:\Python27\lib\site-packages\scipy\interpolate\ndgriddata.py", line 212, in griddata
rescale=rescale)
File "scipy/interpolate/interpnd.pyx", line 840, in scipy.interpolate.interpnd.CloughTocher2DInterpolator.__init__ (scipy\interpolate\interpnd.c:9961)
File "scipy/interpolate/interpnd.pyx", line 78, in scipy.interpolate.interpnd.NDInterpolatorBase.__init__ (scipy\interpolate\interpnd.c:2356)
File "scipy/interpolate/interpnd.pyx", line 123, in scipy.interpolate.interpnd.NDInterpolatorBase._check_init_shape (scipy\interpolate\interpnd.c:3128)
ValueError: invalid shape for input data points
推荐答案
你的数组 x
、y
和 z
是 两个-维,形状为(n, 1)
.griddata
需要 一维 数组(即形状为 (n,)
).
Your arrays x
, y
and z
are two-dimensional, with shape (n, 1)
. griddata
expects one-dimensional arrays (i.e. with shape (n,)
).
要解决此问题,请在从 my_data
中取出数组时在第二个索引位置使用单个索引而不是切片:
To fix this, use a single index instead of a slice in the second index position when you pull the arrays out of my_data
:
x = my_data[1:, 0]
y = my_data[1:, 1]
z = my_data[1:, 2]
这篇关于ValueError:griddata 操作中输入数据点的形状无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!