您可以在每對點之間創建線性路徑;結合與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))
編輯:編輯的問題,現在要求一個非線性插值代替;與
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,
更換update_plot
你不是
編輯#2:關於在下面的評論上的色彩插值查詢,你可以處理,通過matplotlib.collections.Collection.set_color
;具體而言,與
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,
更換上述update_plot
我們最終
關於 「獎金」: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')
關於下面的評論就如何做到這一點的階段編輯:人們可以通過直接在update_plot
結合the composition of paths實現這一目標:
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