2017-02-17 82 views
1

我有個像矩陣:動畫+順利矩陣之間插入

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,但我似乎無法弄清楚。

任何幫助將是偉大的。

獎勵:將在三維空間中工作以及

編輯解決方案:要清楚,我想是沿着每個藍點移動到到達紅點一些非線性的平滑路徑。注 - 上面的例子是由組成的。實際上,只有一堆藍點和一堆紅點。考慮兩個不同散點圖之間的動畫。

+0

你能更具體?你是什​​麼意思「顯示點沿着一條平滑的路徑從紅色移動到藍色」?什麼是不工作? – PrestonH

+0

此外,哪裏會增加一個維度? – fuglede

+0

@PrestonH我在上面添加了一個編輯 – user79950

回答

6

您可以在每對點之間創建線性路徑;結合與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你不是

Nonsensical path between points

編輯#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我們最終

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

關於下面的評論就如何做到這一點的階段編輯:人們可以通過直接在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') 

Example using path composition

+0

謝謝@fuglede!你能發佈你的完整工作代碼嗎?此外,我的意思更像 - 沿着某條曲線*,像樣條曲線或其他東西 – user79950

+0

線性路徑既是曲線也是樣條曲線。此外,在.save('scatterplots.gif',dpi = 80,writer ='imagemagick')'之外,這實際上是完整的工作代碼。 – fuglede

+0

嗯。好吧,謝謝你我會玩弄你給我的 - 最後一個問題 - 爲了讓我也插入顏色,我必須創建一個相應的顏色映射? – user79950