【问题标题】:Getting scalar values of z2(x,y) numpy array along a contour defined by a coordinate list沿坐标列表定义的轮廓获取 z2(x,y) numpy 数组的标量值
【发布时间】:2015-11-18 14:48:05
【问题描述】:

我有一些 x、y、z 数据,其中 z 列包含作为 x、y 坐标函数的标量值:

>>> import numpy as np
>>> import matplotlib.pyplot as plt

>>> # Create some 2D scalar z(x, y) data:
>>> xstepsize, ystepsize = 0.01, 0.01
>>> x = np.arange(0.0, 1.0, xstepsize)
>>> y = np.arange(-0.5, 0.5, ystepsize)
>>> X, Y = np.meshgrid(x, y)
>>> Z1 = np.cos(np.pi*X)**2 + np.sin(np.pi*Y)**2
>>> print Z1.shape
(100L, 100L)

然后,例如,我计算标量相对于 x(或其他一些处理函数)的梯度,以获得一些包含标量值的数组。这里我计算使用 np.gradient,它返回两个二维数组,其中包含相对于 x 和相对于 y 的梯度值:

>>> gx, gy = np.gradient(Z1, xstepsize, ystepsize) 
>>> print gx.shape, gy.shape
(100L, 100L) (100L, 100L)

忽略梯度数据一分钟,我想求轮廓Z1 = 0.2

>>> plt.figure()
>>> # Calculate the contour at z = 0.2
>>> cs = plt.contour(x, y, Z1, [0.2]) # contour values *must* be in a list!!!
>>> p = cs.collections[0].get_paths()[0]
>>> v = p.vertices # <-- is there an alternative step here where I can get the array indices instead, and then use them to get the values from z2? 
>>> # Get the x and y coordinates for points on the contour. 
>>> cx = v[:,0]
>>> cy = v[:,1]

>>> # Plot the data and the resulting contour line:
>>> plt.pcolormesh(X,Y,Z1)
>>> plt.plot(cx, cy)
>>> plt.colorbar()
>>> plt.show()

轮廓有效,我在 1d 数组 cx 和 cy 中有沿轮廓的点的 x 和 y 坐标。 现在,我想将这些坐标插回 gx 或 gy 以获得沿轮廓的渐变!

>>> import scipy.ndimage
>>> scipy.ndimage.map_coordinates(gx, [cx, cy], order=1)
array([ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
       -0.13336999, -0.13340914, -0.13352642, -0.13372136, -0.13399321,
       -0.13411238, -0.13435309, -0.13479776, -0.13509707, -0.13533261,
       -0.13596482, -0.13608176, -0.13671468, -0.13706645, -0.13758994,
       -0.13805113, -0.13862196, -0.13903582, -0.13985807, -0.14002051,
       -0.1410052 , -0.14142457, -0.14198989, -0.14297458, -0.14362177,
       -0.14395927, -0.14494396, -0.14592865, -0.14691333, -0.14789802,
       -0.14888271, -0.1498674 , -0.15085209, -0.15183678, -0.15217427,
       -0.15282147, -0.15380616, -0.15437147, -0.15479085, -0.15577553,
       -0.15593797, -0.15676022, -0.15717409, -0.15774491, -0.15820611,
       -0.1587296 , -0.15908137, -0.15971429, -0.15983123, -0.16046344,
       -0.16069898, -0.16099829, -0.16144295, -0.16168367, -0.16180283,
       -0.16207468, -0.16226963, -0.16238691, -0.16242605,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
        0.        ,  0.        ])

但是,scipy.ndimage.map_coordinates 需要数组索引,并且不能传递 x 和 y 网格数组!所以这不能正常工作。 如何将轮廓的 x、y 坐标传递给 2d 标量数据数组并取回轮廓上的值?

【问题讨论】:

    标签: python numpy scipy contour


    【解决方案1】:

    我相信我已经回答了我自己的问题:

    我所需要的只是使用 x 和 y 向量以及选择的标量数组生成一个新的 scipy.interpolate.interp2d 方法,然后将所需的坐标返回到该方法中:

    >>> from scipy import interpolate
    >>> f = interpolate.interp2d(x, y, gx, kind='cubic')
    >>> results = f(cx, cy)
    [[-2.51109575 -2.51109575 -2.51109575 ..., -2.51109575 -2.51109575
      -2.51109575]
     [-2.50638115 -2.50638115 -2.50638115 ..., -2.50638115 -2.50638115
      -2.50638115]
     [-2.50638115 -2.50638115 -2.50638115 ..., -2.50638115 -2.50638115
      -2.50638115]
     ..., 
     [ 2.50638115  2.50638115  2.50638115 ...,  2.50638115  2.50638115
       2.50638115]
     [ 2.50638115  2.50638115  2.50638115 ...,  2.50638115  2.50638115
       2.50638115]
     [ 2.51109575  2.51109575  2.51109575 ...,  2.51109575  2.51109575
       2.51109575]]
    

    作为一种检查方式,我可以给 x 和 y 一个已知值:

    >>> print f(0.5, 0.0)
    [ -1.38777878e-17]
    

    ... 基本上为零,正如表面上这一点的梯度所预期的那样。

    简单,真的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-09
      • 2016-06-08
      • 1970-01-01
      • 2017-03-11
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多