2016-09-15 5 views
1

Python 3.5、windows 10 Pro。Matplotlib.animation.FuncAnimation using pcolormesh

私は8x8ピクセルの配列を連続的にプロットしようとしています。質問のためにランダムなデータを使用しますが、現実にはシリアルポートから読み取っています。

whileループを使用するとできますが、matplotlib.animation.FuncAnimationに切り替える必要があり、動作させることができません。私はヘルプファイルを見てみて、matplotlib.org の例に従おうとしましたが、私はそれに従うことができませんでした。

私は、FuncAnimationとpcolormeshを使用して8x8ピクセルの配列を連続的にプロットする方法を理解できますか?ここで私はこれまで持っているものです:

import scipy as sp 
import matplotlib.pyplot as plt 
from matplotlib import animation 

plt.close('all') 

y = sp.rand(64).reshape([8,8]) 

def do_something(): 
    y = sp.rand(64).reshape([8,8]) 
    fig_plot.set_data(y) 
    return fig_plot, 

fig1 = plt.figure(1,facecolor = 'w') 
plt.clf() 

fig_plot = plt.pcolormesh(y) 

fig_ani = animation.FuncAnimation(fig1,do_something)  
plt.show() 

あなたは、whileループのコードを見たい場合は、ちょうどので、あなたは、私が再現しようとしている内容を正確に把握、以下を参照してください。

import scipy as sp 
import matplotlib.pyplot as plt 

plt.figure(1) 
plt.clf() 
while True: 
    y = sp.rand(64).reshape([8,8]) 
    plt.pcolormesh(y) 
    plt.show() 
    plt.pause(.000001) 

答えて

0

私は、pcolormeshの代わりにimshowを使用して解決策を見つけることができました。他の誰かが私が持っていたのと同じ問題で苦労している場合、以下の作業コードを掲載しました。

import scipy as sp 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

Hz = sp.rand(64).reshape([8,8]) # initalize with random data 

fig = plt.figure(1,facecolor='w') 

ax = plt.axes() 
im = ax.imshow(Hz) 
im.set_data(sp.zeros(Hz.shape)) 

def update_data(n): 
    Hz = sp.rand(64).reshape([8,8]) # More random data 
    im.set_data(Hz) 
    return 

ani = animation.FuncAnimation(fig, update_data, interval = 10, blit = False, repeat = False) 
fig.show()