我试图在(负对数)双变量高斯下绘制路径(或线)积分。我的目标是制作类似于我找到维基百科的neat little gif不同阶段的内容。
到目前为止,我在3-d中产生了一个(负对数)双变量正态:
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')
产生此结果:
问题:
1)如何从两点产生足够平滑的路径?例如。从我绘制的两个黑点开始。我已经找到了this post,但是对我而言,似乎无法将曲线投影到自己的x,y轴上!
2)这样做之后,我将如何在该路径上绘制一个“窗帘”,直到(负对数)二元高斯?
3)标量值(负对数高斯)和euclidiance距离(x,y上的直线)的额外图2-d(类似于.gif中的最后一个)。
提前致谢!
最佳答案
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)
输出:
关于python - Matplotlib:二元高斯下曲线的曲线路径积分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41046491/