這個問題是我之前遇到的輪廓數據點這裏。griddata輪廓處理時出現錯誤 - x的長度必須是z中的列數,而y的長度必須是行數
[ValueError: invalid shape for input data points in griddata operation
成功執行的GridData,是一個二維陣列,其所需的x和y爲1D和z後,我得到相對於輪廓模塊以下錯誤:
「類型錯誤:長度的x必須是z中的列數,並且y的長度必須是行數。「
對我來說,這在邏輯上是有意義的,因爲必須知道Z的X,Y位置才能輸入正確的數據。對於包含Z數據的點以外的所有其他位置,使用NaN製作正方形二維數組的缺點是,有沒有更優雅的解決方案?
我假設正在生成的griddata包含某種xy在索引基礎上爲z定位,但這可能不是這種情況,事實上xy位置可能需要在數組中精確指定。這似乎正在倍增,我會假定griddata模塊可以處理。
如果任何人都可以從griddata中找出正確的輪廓繪製過程,那將會很有幫助。
一如既往,感謝您的時間和協助。
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
import numpy.ma as ma
from numpy.random import uniform, seed
from numpy import genfromtxt
my_data = genfromtxt('WaterTable.csv', delimiter=',')
# Data imported as a 2D array
z = my_data[1:,2:3]
#Data imported as 1D array
x = my_data[1:, 0]
y = my_data[1:, 1]
xmax = max(x)
xmin = min(x)
ymax = max(y)
ymin = min(y)
# Prepares for import to griddata
xi = np.linspace(xmin, xmax, 2000)
yi = np.linspace(ymin, ymax, 2000)
# grids data
zi = griddata((x, y), z, (xi, yi), method='cubic')
# contour the gridded data
CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
plt.colorbar() # draw colorbar
# plot data points.
plt.scatter(x,y,marker='o',c='b',s=5)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata test (%d points)' % npts)
plt.show()
錯誤拋出:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/GISdev/Irregular_Grid04.py", line 30, in <module>
CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2188, in contour
ret = ax.contour(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 7316, in contour
return mcontour.QuadContourSet(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 1106, in __init__
ContourSet.__init__(self, ax, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 700, in __init__
self._process_args(*args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 1119, in _process_args
x, y, z = self._contour_args(args, kwargs)
File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 1166, in _contour_args
x,y,z = self._check_xyz(args[:3], kwargs)
File "C:\Python27\lib\site-packages\matplotlib\contour.py", line 1208, in _check_xyz
"and length of y must be number of rows.")
TypeError: Length of x must be number of columns in z, and length of y must be number of rows.
我會在'coutour'之前的一個打印語句中檢查'xi,yi,zi'的形狀。更好的方法是在交互式shell中運行這個腳本,並在這個過程中的幾個地方查看變量。 – hpaulj