Interpolating in 3D, plotting with matplotlib - something is going wrong
<p>Here is some toy data:</p>

<pre><code>import pandas as pd
import numpy as np

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

<p>It looks like this (the face is just there for illustration, the top line of each parallelogram is what we are interested in):</p>

<p><a href="http://i.stack.imgur.com/nNXgt.png" rel="nofollow"><img src="http://i.stack.imgur.com/nNXgt.png" alt="enter image description here"></a></p>

<p>Which for the purposes of my application I turn into an ordered dictionary:</p>

<pre><code>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]]))])
</code></pre>

<p>Now I am trying to interpolate in the third dimensions i.e. the one with values <code>[1.62,1.12,0.7,0.56]</code> - I would like to get some idea of what is happening in between.</p>

<p>This is my function:</p>

<pre><code>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()
</code></pre>

<p>But I seem to be messing up the order somehow, I just don't see how:</p>

<pre><code>intstuff = jointInterpolation(myDict)
intstuff.standardGridInterpolation('nearest',True)
</code></pre>

<p><a href="http://i.stack.imgur.com/tSR7n.png" rel="nofollow"><img src="http://i.stack.imgur.com/tSR7n.png" alt="enter image description here"></a></p>

<p>EDIT:</p>

<p><em>I am not entirely sure if this qualifies as a a unstructured grid.</em></p>

<p>Also an updated version with less points, on request.</p>

<pre><code>testDF = pd.DataFrame(np.linspace(0.,10.,40).reshape(10,4),
                      columns=list('abcd'))
</code></pre>

<p><a href="http://i.stack.imgur.com/j5S2y.png" rel="nofollow"><img src="http://i.stack.imgur.com/j5S2y.png" alt="enter image description here"></a></p>

<p>Ok so I figured it out:</p>

<pre><code>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)
</code></pre>

<p>This produces the required figure:</p>

<pre><code>intstuff.standardGridInterpolation('linear',True)
</code></pre>

<p><a href="http://i.stack.imgur.com/qgRk5.png" rel="nofollow"><img src="http://i.stack.imgur.com/qgRk5.png" alt="enter image description here"></a></p>

python matplotlib