用3D插值,用matplotlib绘图 - 出了点问题

时间:2016-09-11 15:50:21

标签: python matplotlib

以下是一些玩具数据:

import pandas as pd
import numpy as np

testDF = pd.DataFrame(np.linspace(100.,200.,40).reshape(10,4),
                      columns=list('abcd'))

它看起来像这样(面部只是为了说明,每个平行四边形的顶线是我们感兴趣的):

enter image description here

出于我的申请的目的,我将变成一个有序的字典:

OrderedDict([('a', array([[ 0.  ,  0.  ,  1.62],
                     [ 1.  ,  1.  ,  1.62],
                     [ 2.  ,  2.  ,  1.62],
                     [ 3.  ,  3.  ,  1.62],
                     [ 4.  ,  4.  ,  1.62],
                     [ 5.  ,  5.  ,  1.62],
                     [ 6.  ,  6.  ,  1.62],
                     [ 7.  ,  7.  ,  1.62],
                     [ 8.  ,  8.  ,  1.62],
                     [ 9.  ,  9.  ,  1.62]])),
             ('b', array([[  0.  ,   1.  ,   1.12],
                     [  1.  ,   2.  ,   1.12],
                     [  2.  ,   3.  ,   1.12],
                     [  3.  ,   4.  ,   1.12],
                     [  4.  ,   5.  ,   1.12],
                     [  5.  ,   6.  ,   1.12],
                     [  6.  ,   7.  ,   1.12],
                     [  7.  ,   8.  ,   1.12],
                     [  8.  ,   9.  ,   1.12],
                     [  9.  ,  10.  ,   1.12]])),
             ('c', array([[  0. ,   4. ,   0.7],
                     [  1. ,   5. ,   0.7],
                     [  2. ,   6. ,   0.7],
                     [  3. ,   7. ,   0.7],
                     [  4. ,   8. ,   0.7],
                     [  5. ,   9. ,   0.7],
                     [  6. ,  10. ,   0.7],
                     [  7. ,  11. ,   0.7],
                     [  8. ,  12. ,   0.7],
                     [  9. ,  13. ,   0.7]])),
             ('d', array([[  0.  ,   9.  ,   0.56],
                     [  1.  ,  10.  ,   0.56],
                     [  2.  ,  11.  ,   0.56],
                     [  3.  ,  12.  ,   0.56],
                     [  4.  ,  13.  ,   0.56],
                     [  5.  ,  14.  ,   0.56],
                     [  6.  ,  15.  ,   0.56],
                     [  7.  ,  16.  ,   0.56],
                     [  8.  ,  17.  ,   0.56],
                     [  9.  ,  18.  ,   0.56]]))])

现在我试图在第三维中插值,即值为[1.62,1.12,0.7,0.56]的那个 - 我想知道两者之间发生了什么。

这是我的功能:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
from matplotlib import cm

class jointInterpolation(object):
    """
    Class for performing various forms of interpolation.
    """
    def __init__(self,trajDict):
        # Concat dictionary into (n_i x D) for all i in speeds.
        D = np.vstack(trajDict.values())
        # Grid the data: [time,angle,velocity]
        self.X = D[:,0]
        self.Y = D[:,1]
        self.Z = D[:,2]    

    def myRegularGridInterpolator(self,df):
        pass
        from scipy.interpolate import RegularGridInterpolator as RGI

    def standardGridInterpolation(self,intMethod,plot=False):
        """
        Interpolate unstructured D-dimensional data.
        """
        # [time,angle,velocity]

        # Time
        xi = np.linspace(self.X.min(),self.X.max(),100)
#         # Angle
        yi = np.linspace(self.Y.min(),self.Y.max(),100)
        # Velocity
#         zi = np.linspace(self.Z.min(),self.Z.max(),100)
        # VERY IMPORTANT: tell matplotlib how the observations are organised
        zi = griddata((self.X, self.Y), self.Z, (xi[None,:], yi[:,None]), method=intMethod)
#         yi = griddata((self.X, self.Z), self.Y, (xi[None,:], zi[:,None]), method=intMethod)        

        if plot:
            fig = plt.figure(figsize=(10,10))
            ax = fig.gca(projection='3d')
            xig, yig = np.meshgrid(xi, yi)
#             xig, zig = np.meshgrid(xi, zi)
            surf = ax.plot_wireframe(xig, yig, zi, #xig, zig, yi
                                     rstride= 4,
                                     cstride= 4,
                                     color = 'darkviolet',
                                     linewidth = 1.5,
                                     alpha=0.7,
                                     antialiased = True)
            plt.show()

但我似乎在某种程度上弄乱了订单,我只是不知道如何:

intstuff = jointInterpolation(myDict)
intstuff.standardGridInterpolation('nearest',True)

enter image description here

编辑:

我不完全确定这是否属于非结构化网格。

也可根据要求提供更少积分的更新版本。

testDF = pd.DataFrame(np.linspace(0.,10.,40).reshape(10,4),
                      columns=list('abcd'))

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我明白了:

class jointInterpolation(object):
    """
    Class for performing various forms of interpolation.
    """
    def __init__(self,trajDict):
        # Concat dictionary into (n_i x D) for all i in speeds.
        D = np.vstack(trajDict.values())
        # Grid the data: [time,angle,velocity]
        self.X = D[:,0]
        self.Y = D[:,1]
        self.Z = D[:,2]    

    def myRegularGridInterpolator(self,df):
        pass
        from scipy.interpolate import RegularGridInterpolator as RGI

    def standardGridInterpolation(self,intMethod,plot=False):
        """
        Interpolate unstructured D-dimensional data.
        """
        # [time,angle,velocity]

        # Velocity
        zi = np.linspace(self.Z.min(),self.Z.max(),100)

        # Angle
        xi = np.linspace(self.X.min(),self.X.max(),100)


        # Velocity
        # VERY IMPORTANT: tell matplotlib how the observations are organised
        yi = griddata((self.Z, self.X), 
                      self.Y,
                      (zi[None,:], xi[:,None]),
                      method=intMethod)

        if plot:
            fig = plt.figure(figsize=(10,10))
            ax = fig.gca(projection='3d')
            zig, yig = np.meshgrid(zi, xi)
            surf = ax.plot_wireframe(zig, yig, yi, #xig, zig, yi
                                     rstride= 5,
                                     cstride= 5,
                                     color = 'darkviolet',
                                     linewidth = 1.5,
                                     alpha=0.7,
                                     antialiased = True)

这产生了所需的数字:

intstuff.standardGridInterpolation('linear',True)

enter image description here