2016-09-15 38 views
1

Python 3.5,windows 10 Pro。使用pcolormesh的Matplotlib.animation.FuncAnimation

我想連續繪製一個8x8像素數組(爲了問題的緣故,我只是使用隨機數據,但是我從串口讀取的是真實的東西)。

我可以做一個while循環,但我需要切換到matplotlib.animation.FuncAnimation,我無法讓它工作。我試過看幫助文件,並試圖從matplotlib.org here的例子,但我一直無法遵循它。

有人可以幫我弄清楚如何使用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() 

如果你想看到的,而循環代碼,只是讓你知道正是我試圖重現,見下文。

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

我能找到使用imshow代替pcolormesh的解決方案。如果其他人正在與我遇到的相同問題鬥爭,我已經發布了下面的工作代碼。

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()