我正在尝试使用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,那么zi看起来是NAN;如果使用method=nearest,那么zi看起来是相同的数字
print xi
print yi
print zi

给予:
XI = [ 1.8783669 - 1.878376 - 1.8783942 - 1.8784033 - 1.8783851 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]]

最佳答案

你有没有试着用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()

关于python - python numpy scipy griddata是nan或所有相同的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21366976/

10-12 22:13