动画+在矩阵之间平滑插值

2024-10-04 03:18:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个矩阵,比如:

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的东西,但我似乎搞不懂。在

任何帮助都会很好。在

好处:一个解决方案,也将在三维工作

编辑:为了清楚起见,我想要的是一些非线性平滑的路径,沿着这个路径每个蓝点移动到红点。注-上面的例子是虚构的。实际上只有一堆蓝点和一堆红点。考虑在两个不同的散点图之间设置动画。在


Tags: import路径herematplotlibasnp动画plt
1条回答
网友
1楼 · 发布于 2024-10-04 03:18:24

您只需在每对点之间创建一条线性路径;将其与^{}组合起来就可以了

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替换为

^{pr2}$

你得到了

Nonsensical path between points

编辑2:关于下面注释中关于颜色插值的查询,您可以通过^{};具体地说,将上面的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

相关问题 更多 >