子图将赢得' t一起显示+ griddata()返回“ nan”值
我有一个9x9矩阵,我知道8个值,在数组 point 中的位置,在数组 values 中具有相应的值。我想插入未知值并将结果粘贴到200x200图像上。
我是Python的初学者,并且在以下方面挣扎:
我的子图不会一起显示。子图(121)和子图(122)应创建一个单个图像,两个图(水平),但我总是得到两个单独的图像。我在做什么错,为什么?
下面的代码是我尝试使用griddata()在200x200网格上插值已知值的尝试。但是,结果, grid_z ,完全填充了 nan 值,我不明白为什么或如何解决此问题。
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
X, Y = np.mgrid[0:1:200j, 0:1:200j]
points = np.array([(3, 8),(5, 8),(4, 6),(4, 4),(2, 4),(6, 4),(3, 0),(5, 0)])
values = [ 1.82907198, 1.69794981, 1.30089053, -0.00452952, 2.32777365, 0.69508469, 2.06540834, 2.1184028 ]
grid_z = griddata(points, values, (X, Y), method='cubic')
plt.figure
plt.subplot(121)
plt.plot(points[:,0], points[:,1], 'ro', markersize=10)
plt.xlim(0, 8)
plt.ylim(0, 8)
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Original')
plt.subplots(122)
plt.imshow(grid_z.T, extent=(0,8,0,8), origin='lower')
plt.title('Cubic')
plt.gcf().set_size_inches(6, 6)
plt.show()
任何帮助将不胜感激!我在网上咨询了数十个类似的帖子,但无法弄清楚我在做什么错。
I have a 9x9 matrix in which I know 8 values, at the positions contained in array points, with corresponding values in array values. I want to interpolate the unknown values and paste the result on a 200x200 image.
I'm a beginner at python and I'm struggling with the following:
My subplots won't display together. Subplot(121) and subplot(122) should create a single image with both plots next to each other (horizontally), but instead I always get two separate images. What am I doing wrong, and why?
The code below is my attempt at using griddata() to interpolate the known values on a 200x200 grid. The result, grid_z, is however completely filled with nan values, and I don't understand why or how I can solve this.
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
X, Y = np.mgrid[0:1:200j, 0:1:200j]
points = np.array([(3, 8),(5, 8),(4, 6),(4, 4),(2, 4),(6, 4),(3, 0),(5, 0)])
values = [ 1.82907198, 1.69794981, 1.30089053, -0.00452952, 2.32777365, 0.69508469, 2.06540834, 2.1184028 ]
grid_z = griddata(points, values, (X, Y), method='cubic')
plt.figure
plt.subplot(121)
plt.plot(points[:,0], points[:,1], 'ro', markersize=10)
plt.xlim(0, 8)
plt.ylim(0, 8)
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Original')
plt.subplots(122)
plt.imshow(grid_z.T, extent=(0,8,0,8), origin='lower')
plt.title('Cubic')
plt.gcf().set_size_inches(6, 6)
plt.show()
Any help would be greatly appreciated! I've consulted dozens of similar posts online but am unable to figure out what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用子图,您需要使用Matplotlib的面向对象的方法(更多信息在这里)。
关于
GRID_Z
包含NAN值,它发生了,因为您选择了错误的离散域。在下面的代码中,我使用x,y = np.mgrid [2:6:200J,0:8:200J]
:这里x将从2开始,到6,而y将从0到8。请注意,此范围涵盖已知点。在这些已知要点之外,您会得到Nan。To work with subplots you need to use Matplotlib's object-oriented approach (more info here).
Regarding
grid_z
containing NaN values, it happens because you selected the wrong discretization domain. In the code below I usedX, Y = np.mgrid[2:6:200j, 0:8:200j]
: here x will start from 2 and goes to 6, and y will go from 0 to 8. Note that this range covers the known points. Outside of these known points you will get Nan.