我正在尝试使用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.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]]
发布于 2014-02-03 06:22:44
你有没有尝试用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()
发布于 2014-09-23 01:00:53
您确定网格中的所有条目都是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)
https://stackoverflow.com/questions/21366976
复制相似问题