如果第一帧中的所有条目都相同,则不显示matplotlib图像动画

2024-04-24 05:32:45 发布

您现在位置:Python中文网/ 问答频道 /正文

如果初始帧中的所有条目都包含相同的值,我似乎无法获得图像的TimedAnimation动画来显示任何内容。例如,如果指示的行仍被注释掉,则以下内容不会显示任何内容。将第一帧更改为包含np.ones(self.shape)也没有任何效果。你知道吗

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

class MyAnimation(anim.TimedAnimation):
    def __init__(self):
        fig = plt.figure()
        self.shape = (10, 10)
        data = np.zeros(self.shape)
        # data.flat[0] = 1 # uncomment this to get the animation to display         
        self.ax = plt.imshow(data)
        super(self.__class__, self).__init__(fig, interval=50, blit=True)

    def _draw_frame(self, framedata):
        data = np.random.rand(*self.shape)
        self.ax.set_data(data)

    def new_frame_seq(self):
        return iter(range(100))

ani = MyAnimation()

关闭blitting似乎没有任何效果。更改后端似乎也没有任何区别;我尝试了Qt4Agg和nbagg(后者通过jupyternotebook4.1.0实现),得到了相同的结果。我在python2.7.11中使用numpy1.10.4和matplotlib1.5.1。你知道吗

是否预期出现上述行为?如果没有,我应该做些什么让动画显示时,第一帧是空白或固体图像?你知道吗


Tags: 图像importself内容datamatplotlibdefas
1条回答
网友
1楼 · 发布于 2024-04-24 05:32:45

设置数据不会重新计算颜色限制。在所有输入值相同的情况下,最小/最大值将自动缩放为相同的值,因此您永远不会看到数据被更新。您可以在init上显式设置限制

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

class MyAnimation(anim.TimedAnimation):
    def __init__(self, ax=None):
        if ax is None:
            fig, ax = plt.subplots()

        self.shape = (10, 10)
        data = np.zeros(self.shape)
        self.im = ax.imshow(data, vmin=0, vmax=1)
        super(self.__class__, self).__init__(ax.figure, interval=50, blit=True)

    def _draw_frame(self, framedata):
        data = np.random.rand(*self.shape)
        self.im.set_data(data)

    def new_frame_seq(self):
        return iter(range(100))

ani = MyAnimation()

或者在_draw_frame方法中使用self.im.set_clim。你知道吗

我也不确定亚分类TimedAnimation是做任何你想做的事情的最简单的方法(FuncAnimation是非常灵活的)。你知道吗

相关问题 更多 >