Matplotlib堆叠动画中的清洁背景

发布于 2025-01-23 16:12:02 字数 1317 浏览 2 评论 0原文

我有一个基本的动画,上面有一个基本的线图,也就是ax.plot(...)。我对其进行了修改,因此它产生了一个堆栈图片,而不是线页图(下面的代码段)。

问题是该情节似乎不再使用每次迭代来清洁背景。您可以在下图中的最高棕色层中看到这一点。即使剧情的其余部分正在更新,但仍然可以看到原始图从顶部伸出来。

我如何获得背景来刷新每次迭代?

这是一个代码片段:

class Display:
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.data = None
        self.lines = []

    def animate(self, i):
        get_data() # This is connected to a random number generator that changes each value a few points at a time

        for index, line in enumerate(self.lines):
            [self.lines[index]] = self.ax.stackplot(range(0, len(self.data[index])), self.data[index])
        return self.lines

    def start(self, data):
        self.data = data

        for index, data in enumerate(self.data):
            self.lines += self.ax.stackplot(range(len(data)), data)

        ani = animation.FuncAnimation(self.fig, self.animate, interval=5, blit=True)
        plt.show()

I have a basic animation with a basic line plot, aka ax.plot(...). I modified it so it produces a stackplot instead of the lineplot (code snippet below).

Problem is the the plot doesn't seem to clean the background with every iteration anymore. You can see this in the top-most brown layer in the images below. Even though the rest of the plot is updating, the original plot never gets erased can still be seen sticking out from the top.

How can I get the background to refresh with every iteration?

enter image description here
enter image description here
Here is a code snippet:

class Display:
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.data = None
        self.lines = []

    def animate(self, i):
        get_data() # This is connected to a random number generator that changes each value a few points at a time

        for index, line in enumerate(self.lines):
            [self.lines[index]] = self.ax.stackplot(range(0, len(self.data[index])), self.data[index])
        return self.lines

    def start(self, data):
        self.data = data

        for index, data in enumerate(self.data):
            self.lines += self.ax.stackplot(range(len(data)), data)

        ani = animation.FuncAnimation(self.fig, self.animate, interval=5, blit=True)
        plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2025-01-30 16:12:02

我找到了解决方法。对于某些类型的情节,似乎不会消除图的最初状态。
因此,您可以绕过问题,但要从空白图开始:

    def start(self, data):
        self.data = data

        for index, data in enumerate(self.data):
            self.lines += self.ax.stackplot([], [])

        ani = animation.FuncAnimation(self.fig, self.animate, interval=5, blit=True)
        plt.show()

I found a workaround. It seems like the very initial state of the plot won't be erased for some types of plot.
So you can bypass the issue but starting with a blank plot:

    def start(self, data):
        self.data = data

        for index, data in enumerate(self.data):
            self.lines += self.ax.stackplot([], [])

        ani = animation.FuncAnimation(self.fig, self.animate, interval=5, blit=True)
        plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文