Animate +在矩阵之间平滑插值

use*_*950 1 python interpolation numpy matplotlib scipy

我有一个点矩阵,如:

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');
Run Code Online (Sandbox Code Playgroud)

这给了我:

在此输入图像描述

我正在尝试生成一个gif /动画,显示沿着从红色到蓝色的平滑路径移动的点.我一直在尝试使用类似于此处讨论的内容和scipy 在此处讨论插值,但我似乎无法弄明白.

任何帮助都会很棒.

额外奖励:也适用于3D的解决方案

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

fug*_*ede 11

您可以在每对点之间创建线性路径; 结合它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))
Run Code Online (Sandbox Code Playgroud)

点之间的线性路径

编辑:现在编辑的问题要求进行非线性插值; 替换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,
Run Code Online (Sandbox Code Playgroud)

你得到了

点之间无意义的路径

编辑#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,
Run Code Online (Sandbox Code Playgroud)

我们最终得到了

插值颜色

关于"奖金":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')
Run Code Online (Sandbox Code Playgroud)

3D示例

编辑以下关于如何分阶段执行此操作的注释:可以通过直接合并路径的组合来实现此目的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')
Run Code Online (Sandbox Code Playgroud)

使用路径组合的示例