从头开始编辑:
我想使用interp2d插入txt数据文件(这次有三列x,y和z)。文件的形状是(1756612,3),数据类似于x,y,z =
[[ 0.1569 0.1161 -0.0432 ]
[ -0.1063 0.2548 0.1365 ]
[ 0.3046 0.07185 0.2372 ]
...,
[ 12.03 0.279 2.227 ]
[ 7.851 0.6597 -0.5134 ]
[ 22.68 0.1968 3.752 ]]
我的目标是插入到2D网格上,其中轴是第1列,即。 x,第2和第3列在一起,y和z。最后两列通过等式f = sqrt(y ^ 2 + z ^ 2)链接,然后我想将其绘制为pcolor类型的图。
但是,我有以下错误,
f = interp2d(x, y, Z, kind='linear')
File "/Users/Reem/anaconda/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 218, in __init__
"Invalid length for input z for non rectangular grid")
ValueError: Invalid length for input z for non rectangular grid
如何修复此错误消息?以及如何正确地使用x列?我无法弄明白。如果需要,我很乐意提供更多信息。
我的代码:
import numpy as np
from scipy.interpolate import interp2d
import matplotlib.pyplot as plt
# Get the data from the file
data = np.genfromtxt('data_pxpypz.txt')
#print(data.shape)
#print(data)
xdata = data[:,0] # 1st col data
ydata = data[:,1] # 2nd col data
zdata = data[:,2] # 3rd col data
# # set the grid using minimum and maximum values of ydata and zdata
x = np.arange(-50, 36, 1)
y = np.arange(-50, 36, 1)
X, Y = np.meshgrid(x, y)
# calculate the function from ydata and zdata
Z = np.sqrt(ydata**2+zdata**2)
# set the new grid as well as the interplating results
x2 = np.arange(-50, 36, 0.1)
y2 = np.arange(-50, 36, 0.1)
f = interp2d(x, y, Z, kind='linear')
Z2 = f(x2, y2)
# #
# #
# plot the results
plt.pcolor(X, Y, Z)
X2, Y2 = np.meshgrid(x2, y2)
plt.pcolor(X2, Y2, Z2)
plt.show()