2016-07-13 22 views
0

matplotlib.animationを使用して3つのサブプロットをアニメーション化する方法を解明しようとしています。私のコードは次のようになります:Matplotlibを使用したカラーバーを使用したimshowサブプロットのアニメーション

# -*- coding: utf-8 -*- 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex='col', sharey='row') 
ax1.set_aspect('equal', 'datalim') 
ax1.set_adjustable('box-forced') 
ax2.set_aspect('equal', 'datalim') 
ax2.set_adjustable('box-forced') 
ax3.set_aspect('equal', 'datalim') 
ax3.set_adjustable('box-forced') 

#fig = plt.figure() 


def f(x, y): 
    return np.sin(x) + np.cos(y) 
def g(x, y): 
    return np.sin(x) + 0.5*np.cos(y) 
def h(x, y): 
    return 0.5*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) 

im1 = plt.imshow(f(x, y), cmap=plt.get_cmap('viridis'), animated=True) 
plt.colorbar(im1) 
im2 = plt.imshow(g(x, y), cmap=plt.get_cmap('viridis'), animated=True) 
plt.colorbar(im2) 
im3 = plt.imshow(h(x, y), cmap=plt.get_cmap('viridis'), animated=True) 
plt.colorbar(im3) 


def updatefig(*args): 
    global x, y 
    x += np.pi/15. 
    y += np.pi/20. 
    im1.set_array(f(x, y)) 
    im2.set_array(g(x, y)) 
    im3.set_array(h(x, y)) 
    return im1,im2,im3 

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

最初のこと - 私は他の2つのプロットをim1よりもどうして見ないのですか? 2番目のこと - サブプロットのそれぞれにカラーバーを正しく追加するにはどうすればいいですか?

答えて

2

これは、作成した他のaxオブジェクトを参照していないために起こります。したがって、同じセット軸を参照し続けます。カラーバーも同様の話。あなたはかなり近くにいるだけで、すべてを適切なオブジェクトに向ける必要があります。見てください。

im1 = ax1.imshow(f(x, y), cmap=plt.get_cmap('viridis'), animated=True) 
fig.colorbar(im1,ax=ax1) 

im2 = ax2.imshow(g(x, y), cmap=plt.get_cmap('viridis'), animated=True) 
fig.colorbar(im2,ax=ax2) 

im3 = ax3.imshow(h(x, y), cmap=plt.get_cmap('viridis'), animated=True) 
fig.colorbar(im3,ax=ax3) 

Single snap shot of 3 animate plots

関連する問題