2017-03-15 12 views
0

ジュピターノートブックで画像のペアをアニメーション化することは可能ですか?画像の二つのリストでサブプロット(matplotlib)で画像を含むアニメーションを生成する方法

greys = io.imread_collection(path_greys) 
grdTru= io.imread_collection(path_grdTru) 

次未経験のコードでは、アニメーションを生成するために失敗した:ところで

for idx in range(1,900): 
    plt.subplot(121) 
    plt.imshow(greys[idx], interpolation='nearest', cmap=plt.cm.gray) 
    plt.subplot(122) 
    plt.imshow(grdTru[idx], interpolation='nearest', cmap=plt.cm.,vmin=0,vmax=3) 
    plt.show() 

(これは、サブプロットのリストを生成)

を、ノートブックにペーストした場合、example found in matplotlib文書が失敗しました。あなたは

%matplotlib notebook 

魔法のコマンドを含める必要がありjupyterノートにthe example作品を作るために

答えて

2

import numpy as np 
%matplotlib notebook 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

fig = plt.figure() 


def f(x, y): 
    return np.sin(x) + np.cos(y) 

x = np.linspace(0, 2 * np.pi, 120) 
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) 

im = plt.imshow(f(x, y), animated=True) 


def updatefig(*args): 
    global x, y 
    x += np.pi/15. 
    y += np.pi/20. 
    im.set_array(f(x, y)) 
    return im, 

ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True) 
plt.show() 

あなたの画像のリストに簡単に変更できます。

matplotlibバージョン2.1以降では、JavaScriptアニメーションをインラインで作成するオプションもあります。

from IPython.display import HTML 
HTML(ani.to_jshtml()) 

コンプリート例:

import numpy as np 
%matplotlib inline 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

def f(x, y): 
    return np.sin(x) + np.cos(y) 

x = np.linspace(0, 2 * np.pi, 120) 
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1) 

im = plt.imshow(f(x, y), animated=True); 


def updatefig(*args): 
    global x, y 
    x += np.pi/15. 
    y += np.pi/20. 
    im.set_array(f(x, y)) 
    return im, 

ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True) 

from IPython.display import HTML 
HTML(ani.to_jshtml()) 
関連する問題