子图将赢得' t一起显示+ griddata()返回“ nan”值

发布于 2025-01-24 04:10:16 字数 1126 浏览 0 评论 0原文

我有一个9x9矩阵,我知道8个值,在数组 point 中的位置,在数组 values 中具有相应的值。我想插入未知值并将结果粘贴到200x200图像上。

我是Python的初学者,并且在以下方面挣扎:

  1. 我的子图不会一起显示。子图(121)和子图(122)应创建一个单个图像,两个图(水平),但我总是得到两个单独的图像。我在做什么错,为什么?

  2. 下面的代码是我尝试使用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:

  1. 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?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2025-01-31 04:10:16

要使用子图,您需要使用Matplotlib的面向对象的方法(更多信息在这里)。

关于GRID_Z包含NAN值,它发生了,因为您选择了错误的离散域。在下面的代码中,我使用x,y = np.mgrid [2:6:200J,0:8:200J]:这里x将从2开始,到6,而y将从0到8。请注意,此范围涵盖已知点。在这些已知要点之外,您会得到Nan。

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize

X, Y = np.mgrid[2:6:200j, 0:8:200j]

points = np.array([(3, 8),(5, 8),(4, 6),(4, 4),(2, 4),(6, 4),(3, 0),(5, 0)])
values =  np.array([ 1.82907198,  1.69794981,  1.30089053, -0.00452952,  2.32777365,  0.69508469,  2.06540834,  2.1184028 ])

grid_z = griddata(points, values, (X, Y), method='linear')

f, axs = plt.subplots(1, 2, tight_layout=True)

cmap = "jet"
norm = Normalize(vmin=values.min(), vmax=values.max())
scatter = axs[0].scatter(points[:,0], points[:,1], c=values, norm=norm, cmap=cmap)
axs[0].set_xlim(0, 8)
axs[0].set_ylim(0, 8)
axs[0].set_aspect('equal', adjustable='box')
axs[0].set_title('Original')

axs[1].imshow(grid_z.T, extent=(0,8,0,8), origin='lower', cmap=cmap)
axs[1].set_title('Cubic')

f.colorbar(scatter)
f.axes[2].set_ylabel("values")
plt.show()

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 used X, 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.

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize

X, Y = np.mgrid[2:6:200j, 0:8:200j]

points = np.array([(3, 8),(5, 8),(4, 6),(4, 4),(2, 4),(6, 4),(3, 0),(5, 0)])
values =  np.array([ 1.82907198,  1.69794981,  1.30089053, -0.00452952,  2.32777365,  0.69508469,  2.06540834,  2.1184028 ])

grid_z = griddata(points, values, (X, Y), method='linear')

f, axs = plt.subplots(1, 2, tight_layout=True)

cmap = "jet"
norm = Normalize(vmin=values.min(), vmax=values.max())
scatter = axs[0].scatter(points[:,0], points[:,1], c=values, norm=norm, cmap=cmap)
axs[0].set_xlim(0, 8)
axs[0].set_ylim(0, 8)
axs[0].set_aspect('equal', adjustable='box')
axs[0].set_title('Original')

axs[1].imshow(grid_z.T, extent=(0,8,0,8), origin='lower', cmap=cmap)
axs[1].set_title('Cubic')

f.colorbar(scatter)
f.axes[2].set_ylabel("values")
plt.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文