网格数据错误
(改编自:Matplotlib contour from xyz data: griddata invalid index)
我有一些与一组坐标相对应的值的矩阵。我想从外部文件加载数据,并使用griddata对象在点之间进行插值。但是,在运行代码时出现错误:
来自griddata的“ builtins.IndexError:索引0超出了轴0的大小为0的范围”。我不懂这啥意思?
一段示例代码:
def main():
#https://stackoverflow.com/questions/13781025/matplotlib-contour-from-xyz-data-griddata-invalid-index
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
ndata = 121
ny, nx = 100, 200
xmin, xmax = 0, 10
ymin, ymax = 0, 10
Data = np.loadtxt('Data.dat')
Data = Data.flatten(1)
x = np.array([j for i in np.arange(0,11,1) for j in np.arange(0,11,1)])
y = np.array([j for i in np.arange(0,11,1) for j in np.arange(0,11,1)])
#x = np.random.randint(xmin, xmax, ndata)
#y = np.random.randint(ymin, ymax, ndata)
xi = np.linspace(xmin, xmax, nx)
yi = np.linspace(ymin, ymax, ny)
zi = ml.griddata(x, y, Data, xi, yi)
plt.contour(xi, yi, zi, 15, linewidths = 0.5, colors = 'k')
plt.pcolormesh(xi, yi, zi, cmap = plt.get_cmap('rainbow'))
plt.scatter(x, y, marker = 'o', c = 'b', s = 5, zorder = 10)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.show()
可以从以下地址获取Data.dat:http://pastebin.com/Uk8SHA1F
请注意,它如何与提供x或y的推导结合另一个坐标作为随机坐标(带注释)一起使用,但是当同时对x和y使用推导时却不起作用?
最佳答案
来自griddata的“ builtins.IndexError:索引0超出了轴0的大小为0的范围”。我不懂这啥意思?index 0 is out of bounds for axis 0 with size 0
如果期望使用2-D且提供了1-D数组,并且第一个索引超出范围,则会出现此错误。例如:
>>> np.array([])[0,:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index 0 is out of bounds for axis 0 with size 0
>>> np.array([2,4])[3,:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index 3 is out of bounds for axis 0 with size 2
请注意,如果您没有为第二轴指定任何值,则会得到另一个错误:
>>> np.array([])[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index out of bounds
关于python-3.x - python griddata中的错误:“builtins.IndexError:索引0超出了尺寸0的轴0的范围”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15001900/