Вы можете просто создать линейный путь между каждой из пар точек; сочетая это с 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))

Edit: Отредактированные вопросы теперь просят нелинейную интерполяцию вместо; замена 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,
вы получите вместо

Edit # 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,
мы в конечном итоге с

относительно "бонус": 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')

Редактировать относительно комментарий ниже о том, как сделать это в несколько этапов: Можно достичь этого путем включения 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')

Можете ли вы быть более конкретно? Что вы имеете в виду «показывая точки, движущиеся вдоль некоторого гладкого пути от красного до синего»? Что не работает? – PrestonH
Кроме того, где бы добавить измерение? – fuglede
@PrestonH Я добавил, что редактирование выше. – user79950