我正在尝试使用numpy,matplotlib plyplot和scipy在python中绘制具有不均匀间距数据的轮廓。
鉴于以下代码段,为什么zi要么为空,要么全部都是相同的值?
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
lon_min = 1.8783669
lon_max = 1.8792678
lat_min = 57.45827
lat_max = 57.459293
x = [ 520.99012099,652.23665224,800.,0.,520.99012099
652.23665224,800.,0.,520.99012099,652.23665224 ...]
y = [ 0.,379.47214076,437.53665689,600.,0.
379.47214076,437.53665689,600.,0.,379.47214076 ...]
z = [ 56.6,56.6,56.6,56.6,45.3,45.3,45.3,45.3,57.8,57.8 ...]
xi = np.linspace(lon_min,lon_max,10)
yi = np.linspace(lat_min,lat_max,10)
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='nearest')
plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k') # this is blank or all the same colour because zi is either nan or all the same number depending on the method I use.
应用一点调试,如果我使用method = cubic / linear,或者如果我使用method = nearest
,则zi看起来像是NANprint xi
print yi
print zi
给出: xi = [1.8783669 1.878376 1.8783851 1.8783942 1.8784033 1.8784124 1.8784215 1.8784306 1.8784397 1.8784488 1.8784579 1.878467 1.8784761 1.8784852 1.8784943 1.8785034 1.8785125 ....]
yi = [57.45827 57.45828033 57.45829067 57.458301 57.45831133
57.45832167 57.458332 57.45834233 57.45835267 57.458363
57.45837333 57.45838367 57.458394 57.45840433 57.45841467
57.458425 57.45843533 57.45844567 57.458456 57.45846633 .... ]
zi = [[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
...,
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]]
zi = [[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7]
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7]
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7]
...,
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7]
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7]
[ 46.7 46.7 46.7 ..., 46.7 46.7 46.7]]
答案 0 :(得分:0)
您是否尝试使用tricontour直接绘制数据轮廓?
http://matplotlib.org/api/pyplot_api.html?highlight=tricontour#matplotlib.pyplot.tricontour
plt.tricontour(x, y, z)
或者如果您需要查看底层网格:
import matplotlib.tri as mtri
triang = mtri.Triangulation(x, y)
plt.tricontour(triang, z)
plt.triplot(triang)
在您的情况下,三角测量实际上减少为3个三角形,因为您有重复的点,因此必须为相同的位置选择一个唯一的z值。对于填充轮廓,您可以更好地了解使用tricontourf
会发生什么。重复点还解释了为什么插值例程可能会对此数据集造成麻烦...
现在,如果您为4个数据点中的每个数据点随机选择1个任意z值
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
x = np.array([520.99012099, 652.23665224, 800., 0.])
y = np.array([0., 379.47214076, 437.53665689, 600.])
z = np.array([45.3, 57.8, 57.8, 57.8])
triang = mtri.Triangulation(x, y)
refiner = mtri.UniformTriRefiner(triang)
refi_triang, refi_z = refiner.refine_field(z, subdiv=4)
levels = np.linspace(45, 61, 33)
CS_colors = plt.tricontourf(refi_triang, refi_z, levels=levels)
plt.triplot(triang, color="white")
plt.colorbar()
CS_lines = plt.tricontour(refi_triang, refi_z, levels=levels, colors=['black'])
plt.clabel(CS_lines, CS_lines.levels, inline=True, fontsize=10)
plt.show()
答案 1 :(得分:0)
您确定网格中的所有条目都是 NaN 。要验证这一点,请运行此代码
nan = 0
notnan = 0
for index,x in np.ndenumerate(zi):
if not np.isnan(x):
notnan+=1
else:
nan+=1
print 'nan ', nan
print 'not nan', notnan
print 'sum ', nan+notnan
print 'shape ', zi.shape
您可以使用以下命令绘制zi:
plt.imshow(zi)