Animate +在矩阵之间平滑插值

时间:2017-02-17 20:13:29

标签: python numpy matplotlib scipy interpolation

我有一个点矩阵,如:

import numpy as np
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
%matplotlib inline

originalPoints = np.asarray([[1,2,3,4,5,6],[2,4,6,8,10,12]])
newPoints = np.asarray([[1,2,3,4,5,6],[2,4,6,8,10,12]]) + 20
plt.scatter(originalPoints[0,:],originalPoints[1,:], color='red');
plt.scatter(newPoints[0,:],newPoints[1,:], color='blue');

这让我:

enter image description here

我正在尝试生成一个gif /动画,显示沿着从红色到蓝色的平滑路径移动的点。我一直试图使用类似what is discussed here和scipy的interpolate discussed here之类的东西,但我似乎无法弄明白。

任何帮助都会很棒。

奖励:也可以在3D中使用的解决方案

编辑:要清楚,我想要的是一些非线性平滑路径,每个蓝点移动到达红点。注意 - 上面的例子组成了。实际上只有一堆蓝点和一堆红点。考虑在两个不同的散点图之间制作动画。

1 个答案:

答案 0 :(得分:9)

您可以在每对点之间创建线性路径;将其与matplotlib.animation.FuncAnimation结合起来就像

import matplotlib.animation as animation

def update_plot(t):
    interpolation = originalPoints*(1-t) + newPoints*t
    scat.set_offsets(interpolation.T)
    return scat,

fig = plt.gcf()
plt.scatter(originalPoints[0,:],originalPoints[1,:], color='red')
plt.scatter(newPoints[0,:],newPoints[1,:], color='blue')
scat = plt.scatter([], [], color='green')
animation.FuncAnimation(fig, update_plot, frames=np.arange(0, 1, 0.01))

Linear path between points

编辑:编辑后的问题现在要求进行非线性插值;用

替换update_plot
noise = np.random.normal(0, 3, (2, 6))
def update_plot(t):
    interpolation = originalPoints*(1-t) + newPoints*t + t*(1-t)*noise
    scat.set_offsets(interpolation.T)
    return scat,

你得到了

Nonsensical path between points

编辑#2:关于下面评论中关于颜色插值的查询,您可以通过matplotlib.collections.Collection.set_color处理;具体而言,用

替换上述update_plot
def update_plot(t):
    interpolation = originalPoints*(1-t) + newPoints*t + t*(1-t)*noise
    scat.set_offsets(interpolation.T)
    scat.set_color([1-t, 0, t, 1])
    return scat,

我们最终以

结束

Interpolation with colors

关于“奖金”:3D案例大致相似;

a = np.random.multivariate_normal([-3, -3, -3], np.identity(3), 20)
b = np.random.multivariate_normal([3, 3, 3], np.identity(3), 20)

def update_plot(t):
    interpolation = a*(1-t) + b*t
    scat._offsets3d = interpolation.T
    scat._facecolor3d = [1-t, 0, t, 1]
    return scat,

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(a[:, 0], a[:, 1], a[:, 2], c='r')
ax.scatter(b[:, 0], b[:, 1], b[:, 2], c='b')
scat = ax.scatter([], [], [])
ani = animation.FuncAnimation(fig, update_plot, frames=np.arange(0, 1, 0.01))
ani.save('3d.gif', dpi=80, writer='imagemagick')

3D example

关于如何分阶段执行此操作的以下评论进行修改:可以通过将the composition of paths直接合并到update_plot中来实现此目的:

a = np.random.multivariate_normal([-3, -3, -3], np.identity(3), 20)
b = np.random.multivariate_normal([3, 3, 3], np.identity(3), 20)
c = np.random.multivariate_normal([-3, 0, 3], np.identity(3), 20)

def update_plot(t):
    if t < 0.5:
        interpolation = (1-2*t)*a + 2*t*b
        scat._facecolor3d = [1-2*t, 0, 2*t, 1]
    else:
        interpolation = (2-2*t)*b + (2*t-1)*c
        scat._facecolor3d = [0, 2*t-1, 2-2*t, 1]
    scat._offsets3d = interpolation.T
    return scat,

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(a[:, 0], a[:, 1], a[:, 2], c='r')
ax.scatter(b[:, 0], b[:, 1], b[:, 2], c='b')
ax.scatter(c[:, 0], c[:, 1], c[:, 2], c='g')
scat = ax.scatter([], [], [])
ani = animation.FuncAnimation(fig, update_plot, frames=np.arange(0, 1, 0.01))
ani.save('3d.gif', dpi=80, writer='imagemagick')

Example using path composition