【问题标题】:Matplotlib.animation.FuncAnimation using pcolormeshMatplotlib.animation.FuncAnimation 使用 pcolormesh
【发布时间】:2016-09-15 23:21:54
【问题描述】:

Python 3.5,Windows 10 专业版。

我正在尝试连续绘制一个 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()

如果您想查看 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)

【问题讨论】:

    标签: python-3.x animation matplotlib


    【解决方案1】:

    我能够使用 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()
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2022-11-09
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2012-09-01
      • 2017-10-04
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多