2016-12-08 9 views
1

(負の対数)二変量ガウスの下でパス(または線)積分をプロットしようとしています。私はこれのさまざまな段階のように見えるものを目指していますneat little gif私はウィキペディアを見つけました。Matplotlib:二変量ガウスの曲線のプロットパス積分

これまでのところ、私は3-Dで2変量正規(負の対数)を製造してきました:

import matplotlib 
import random 
import numpy as np 
import seaborn as sns 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.mlab import bivariate_normal 

sns.set(style="white", palette="muted", color_codes=True) 

%matplotlib inline 

def pathIntegral(): 

    # Bivariate gaussian 

    a = np.linspace(-15, 15, 100) 
    b = a 
    X,Y = np.meshgrid(a, b) 
    Z = bivariate_normal(X, Y) 
    surprise_Z = -np.log(Z) 

    # Figure 

    fig = plt.figure(figsize=(10, 7))  

    ax = fig.gca(projection='3d')  
    surf = ax.plot_surface(X, Y, surprise_Z, rstride=1, cstride=1, 
     cmap = plt.cm.gist_heat_r, alpha=0.1, linewidth=0.1) 

    ax.scatter(-5, -5, 0, c='k', marker='o') 
    ax.scatter(5, 5, 0, c='k', marker='o') 

この生成:

enter image description here


質問:

1)どのようにip 2つのポイントから十分に滑らかな道を歩く?例えば。私が散らばって描いた2つの黒い点から。私はthis postを見つけましたが、私の人生のために、私自身のx、y軸にその曲線を投影するように見えません!

2)そうしてしまえば、どのようにしてそのカーテンを(負の対数)二変量ガウス分布まで描くことができますか?

3)スカラー値(負のlogガウス分布)とeuclidiance距離(x、yの点線)の余分なプロット2-d(.gifの最後のもののようなもの)はボーナス問題です。

ありがとうございます!

+0

あなたは "十分に滑らか" とはどういう意味ですか? 2点間の最も滑らかなパスは単なる直線です。あなたはいくつかのポイントを選んでそれらを補間したいかもしれませんか?リンク先のポストは、複数の点を通過するパラメトリック曲線を生成する方法を示しています(http://stackoverflow.com/q/14344099/1461210も参照)。 –

+0

パスが事前定義されている場合は、そのパスは何ですか? – eyllanesc

答えて

1
import matplotlib.pyplot as plt 
import numpy as np 
import seaborn as sns 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.mlab import bivariate_normal 
import scipy.interpolate 


def func(X, Y): 
    # Bivariate gaussian 
    return -np.log(bivariate_normal(X, Y)) 

sns.set(style="white", palette="muted", color_codes=True) 


def create_smoth_path(points): 
    data = np.array(points).T 

    tck, u = scipy.interpolate.splprep(data, s=0) 
    unew = np.arange(0, 1.01, 0.01) 
    out = scipy.interpolate.splev(unew, tck) 
    return out 


def pathIntegral(path, func_): 
    x = path[0] 
    y = path[1] 
    z = func_(x, y) 
    z1 = np.zeros((2, x.shape[0])) 
    z1[1] = z 

    x1 = np.tile(x, (2, 1)) 
    y1 = np.tile(y, (2, 1)) 

    start = min(x.min(), y.min())-2 
    stop = max(x.max(), y.max())+2 
    a = np.arange(start, stop, 0.5) 
    b = a 
    X, Y = np.meshgrid(a, b) 
    Z = func_(X, Y) 

    ax = plt.subplot2grid((3,1), (0,0), rowspan=2, projection='3d') 
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.gist_heat_r, alpha=0.2, linewidth=0.1) 

    ax.plot_surface(x1, y1, z1, rstride=1, cstride=1, color='g') 
    ax.scatter(x[0], y[0], 0, c='k', marker='o') 
    ax.scatter(x[-1], y[-1], 0, c='k', marker='o') 
    ax.plot(x1, y1, color='b') 
    dx = np.diff(x) 
    dy = np.diff(y) 
    dr = np.sqrt(dx**2 + dy**2) 
    r = np.concatenate((np.zeros(1), np.cumsum(dr))) 
    ax = plt.subplot2grid((3,1), (2,0)) 
    ax.set_xlim([np.min(r), np.max(r)]) 
    plt.plot(r, z, 'r') 
    plt.xlabel('r') 
    plt.ylabel('z(x, y)') 
    plt.show() 

# path 
points = [(-5, -6), (-1, -3), (1, -2), (2, 1), (0, 0), (2, 2), (4, 3), (4, 3.5), (5, 5)] 

r = create_smoth_path(points) 
pathIntegral(r, func) 

出力:

enter image description here