Matplotlib堆叠动画中的清洁背景
我有一个基本的动画,上面有一个基本的线图,也就是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?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方法。对于某些类型的情节,似乎不会消除图的最初状态。
因此,您可以绕过问题,但要从空白图开始:
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: