gpt4 book ai didi

python numpy scipy griddata 是 nan 或所有相同的值

转载 作者:行者123 更新时间:2023-11-28 19:22:40 27 4
gpt4 key购买 nike

我正在尝试使用 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,则看起来是所有相同的数字

print xi
print yi
print zi

给出: 习 = [ 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]]

最佳答案

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

enter image description here

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com