【问题标题】:Creating grid and interpolating (x,y,z) for contour plot sagemath为等高线图 sagemath 创建网格和插值 (x,y,z)
【发布时间】:2012-07-12 09:48:17
【问题描述】:

!我有 (x,y,z) 形式的值。通过创建一个 list_plot3d 图,我可以清楚地看到它们的间距不是很均匀。它们通常在 xy 平面上形成 3 到 5 个点的小“斑点”。因此,为了使插值和最终的“轮廓”图更好,或者我应该说更平滑(?),我是否必须创建一个矩形网格(如棋盘上的正方形),以便数据块以某种方式“平滑”?我知道这对某些人来说可能是微不足道的,但我是第一次尝试这个,我有点挣扎。我一直在查看像 scipy.interplate.interp2d 这样的 scipy 包,但最后生成的图表非常糟糕。对于像我这样的业余爱好者来说,也许是一个关于 sagemath 中 2d 插值的简短教程?一些忠告?谢谢。

编辑:

https://docs.google.com/file/d/0Bxv8ab9PeMQVUFhBYWlldU9ib0E/edit?pli=1

这主要是它与此消息一起生成的图表类型:

Warning:     No more knots can be added because the number of B-spline
coefficients
    already exceeds the number of data points m. Probably causes:
either
    s or m too small. (fp>s)
    kx,ky=3,3 nx,ny=17,20 m=200 fp=4696.972223 s=0.000000

要得到这张图,我只需要运行这个命令:

f_interpolation = scipy.interpolate.interp2d(*zip(*matrix(C)),kind='cubic')
               plot_interpolation = contour_plot(lambda x,y:
                   f_interpolation(x,y)[0], (22.419,22.439),(37.06,37.08) ,cmap='jet', contours=numpy.arange(0,1400,100), colorbar=True)

               plot_all = plot_interpolation

               plot_all.show(axes_labels=["m", "m"])

matrix(c) 可以是一个巨大的矩阵,例如 10000 X 3,甚至更像是 1000000 x 3。即使像我现在所附的图片那样,只有 matrix(C) 的数据较少,坏图的问题仍然存在200 x 3。这就是为什么我开始认为除了程序可能出现故障之外,我使用此命令的方法可能完全错误,因此我有理由寻求有关使用网格的建议和不仅仅是将我的数据“扔”到命令中。

【问题讨论】:

  • 你能把剧情贴出来,描述一下有什么问题吗?

标签: numpy scipy interpolation contour sage


【解决方案1】:

我在使用 scipy.interpolate.interp2d 函数时遇到了类似的问题。我的理解是,问题的出现是因为 interp1d/interp2d 和相关函数使用旧的 FITPACK 包装进行基础计算。我能够使用样条函数解决与您类似的问题,该函数依赖于更新的 FITPACK 包装。样条函数可以被识别,因为它们在http://docs.scipy.org/doc/scipy/reference/interpolate.html 的名称中似乎都有大写字母。在 scipy 安装中,这些较新的函数似乎位于 scipy/interpolate/fitpack2.py 中,而使用旧包装的函数位于 fitpack.py 中。

出于您的目的,我相信您想要的是 RectBivariateSpline。下面是一些实现 RectBivariateSpline 的示例代码:

import numpy as np
from scipy import interpolate

# Generate unevenly spaced x/y data for axes
npoints = 25
maxaxis = 100
x = (np.random.rand(npoints)*maxaxis) - maxaxis/2.
y = (np.random.rand(npoints)*maxaxis) - maxaxis/2.
xsort = np.sort(x)
ysort = np.sort(y)

# Generate the z-data, which first requires converting
# x/y data into grids
xg, yg = np.meshgrid(xsort,ysort)
z = xg**2 - yg**2

# Generate the interpolated, evenly spaced data
# Note that the min/max of x/y isn't necessarily 0 and 100 since
# randomly chosen points were used. If we want to avoid extrapolation,
# the explicit min/max must be found
interppoints = 100
xinterp = np.linspace(xsort[0],xsort[-1],interppoints)
yinterp = np.linspace(ysort[0],ysort[-1],interppoints)

# Generate the kernel that will be used for interpolation
# Note that the default version uses three coefficients for
# interpolation (i.e. parabolic, a*x**2 + b*x +c). Higher order
# interpolation can be used by setting kx and ky to larger 
# integers, i.e. interpolate.RectBivariateSpline(xsort,ysort,z,kx=5,ky=5)
kernel = interpolate.RectBivariateSpline(xsort,ysort,z)

# Now calculate the linear, interpolated data
zinterp = kernel(xinterp, yinterp)

【讨论】:

  • 看来这个问题也可以用相关函数解决,scipy.interpolate.griddata:stackoverflow.com/questions/11348708/…
  • 感谢您的回复。我还是不明白这一点。也许一个更简单的问题会更好地解决问题。如果我拥有的值是(经度,纬度,高度)并且我只想要一个具有相同高度轮廓的等高线图,我会在 sagemath 中做什么?我会使用 interp2d、RectBivariateSpline 还是其他东西?
  • 我从来没有使用过 sagemath,但假设所有的函数调用都是相同的(我不知道),最后两行代码是您插入数据所需要的。这就是这个脚本的重点。等高线图要求经度和纬度数据是与高度相同大小的网格,因此您必须使用 numpy 的 meshgrid 命令,然后使用 matplotlib 的等高线图绘制数据。等值线图的绘制已在本网站的多个问题中讨论。
  • 在 scipy 食谱中有一个示例,它使用了 griddata 并在此处绘制了两种不同类型的等高线图:scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data,它使用了我上面提到的 griddata 命令。有趣的是,不需要 meshgrid 命令。我发誓我一直都必须使用它,但也许我弄错了,或者自从我上次检查以来这个要求已经改变。同样,我还没有接触过 sagemath,所以你必须弄清楚这些命令是如何映射到那里的命名空间的。
  • 运行上面的 griddata 食谱示例后,我意识到为什么没有 meshgrid 命令——因为 z 数据也是一维数组。因此,我对网格网格的使用只是部分不正确。进入轮廓的 x/y/z 数据必须是相同的大小。如果 z 数据是线性的,则 x 和 y 必须是线性的。但是如果z是M x N矩阵,x是长度为N的线性矩阵,y是长度为M的线性矩阵,则必须使用meshgrid。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 2018-07-17
相关资源
最近更新 更多