Python 3.x Matplotlib.animation.FuncAnimation使用pcolormesh
Python3.5,Windows10Pro 我正试图连续绘制一个8x8像素阵列(为了回答这个问题,我将只使用随机数据,但实际上我是从串行端口读取的) 我可以使用while循环来完成,但是我需要切换到matplotlib.animation.FuncAnimation,我无法让它工作。我试着查看帮助文件,并尝试按照matplotlib.org中的示例进行操作,但我一直无法做到这一点 有人能帮我弄清楚如何使用FuncAnimation和pcolormesh连续绘制8x8像素阵列吗?以下是我到目前为止得到的信息:Python 3.x Matplotlib.animation.FuncAnimation使用pcolormesh,python-3.x,animation,matplotlib,Python 3.x,Animation,Matplotlib,Python3.5,Windows10Pro 我正试图连续绘制一个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)
我能够使用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()
我能够使用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()