【问题标题】:python numpy scipy griddata is nan or all the same valuepython numpy scipy griddata 是 nan 或所有相同的值
【发布时间】:2014-02-17 11:19:45
【问题描述】:

我正在尝试使用 numpy、matplotlib plyplot 和 scipy 在 python 中绘制具有不均匀数据的轮廓。

给定以下代码 sn-p,为什么 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,则都是相同的数字

print 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]]

【问题讨论】:

  • 你能把它变成真正的 Python 代码吗?处理几乎不可运行的代码是非常烦人的。有关正确输出数组的帮助,请参阅this。人们更有可能以这种方式回答问题。
  • griddata 调用对scipy.interpolate.griddata也无效
  • 从文档看来,您似乎没有向 griddata 传递正确的值 docs.scipy.org/doc/scipy/reference/generated/… griddata(points, values, xi, method='linear', fill_value=nan) 您将 5 个值传递给griddata,你知道你认为你传递了哪些值
  • 更新:将 scipy 和 numpy 更新到较新版本后,我现在得到所有相同的值(如果我使用 method=nearest)或 nan 如果我使用 method=cubic/linear - 知道为什么吗? print 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 ..., 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]] print zi (cubic) [[ nan nan nan ..., nan nan nan ] ..., [楠楠楠 ..., 楠楠楠]]
  • @SamMaj 这有没有得到解决。谢谢!

标签: python numpy matplotlib scipy


【解决方案1】:

您是否尝试使用 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()

【讨论】:

  • 如果我只是做 plt.tricontour(x,y,z) 我得到一个空白图像,底层网格显示为三角形:postimg.org/image/xczqqcpcf
  • 谢谢 GBy,在遵循您的代码示例后,我也明白了,如果我随后修改我的数据以具有唯一的 x,y 坐标,那么我会得到这个postimg.org/image/6o2hne2fn,我认为它看起来更好(虽然我不'实际上不知道它应该是什么样子!)如果我然后尝试使用 plt.tricontour(x, y, z) 创建轮廓,我会得到这个 postimg.org/image/ti01adgib/0233c1ac 我现在如何实际得到轮廓?
  • 如果有帮助很高兴。我不确定你所说的“轮廓”到底是什么意思,因为对我来说你的第二张图片 is 是一个轮廓?此外,这将有助于提供您的更新数据。
  • 哦,也许我的术语不正确,这就是我认为的轮廓,也是我想要实现的目标:idlcoyote.com/cg_tips/outcontourbar.png 关于数据,我只是手动选择了一组随机唯一点从我的数据中查看唯一性是否是问题所在,并且看起来是阻止系统绘制图表的原因,但我不确定我将使用什么来创建类似于我提供的链接的图表?
  • 好的,我明白了。这很简单,您只需将tricontourtricontourfclabel 组合起来。查看更新。
【解决方案2】:

您确定网格中的所有条目都是 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)

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 2014-03-28
    • 2011-09-17
    • 1970-01-01
    • 2020-08-19
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多