动画 Matplotlib 面板 - blit 留下旧帧 [英] Animating Matplotlib panel - blit leaves old frames behind

查看:42
本文介绍了动画 Matplotlib 面板 - blit 留下旧帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wxPython 应用程序,其中包含一个 matplotlib 面板(由 wxmpl 提供,虽然我已经看到了与普通 FigureCanvasWxAgg 画布相同的内容).

I have a wxPython application which contains a matplotlib panel (courtesy of wxmpl, though I've seen the same with a plain FigureCanvasWxAgg canvas).

我想为面板中的一个情节制作动画,过去我曾做过类似的事情.我建议这样做:

I'd want to animate one of the plots in the panel, and in the past I've made similar things happen. The way I'm doing it is the suggested:

  • 复制背景
  • 情节
  • [...]
  • 恢复背景
  • 更新行数据
  • 绘画艺术家

问题在于,情节并没有被背景恢复覆盖",而是留在那里,可以理解的是,整个事情看起来一团糟.

The problem is that the plots, instead of being "overwritten" by the background restoring, stay there and the whole things understandably looks a mess.

一些(简化的)代码:

fig = self.myPanel.get_figure()
ax_top = fig.add_subplot(211)
ax_bottom = self.fig.add_subplot(212)
canvas = fig.canvas
canvas.draw()
bg_top = canvas.copy_from_bbox(ax_top.bbox)
bg_bottom = canvas.copy_from_bbox(ax_bottom.bbox)
line, = ax_bottom.plot(x, y, 'k', animated=True)

然后,在更新时:

canvas.restore_region(bg_bottom)
line.set_ydata(new_y)
ax_bottom.draw_artist(line)
canvas.blit(ax_bottom.bbox)

绘制新线(而且非常快!:),但由于某种原因,它发生在旧线上.有人能猜到为什么吗?

The new line gets drawn (and very fast! :), but for some reason it happens over the old line. Can anybody guess why?

推荐答案

已根据请求添加为答案 :)

Added as an answer, by request :)

在调用 fig.canvas.copy_from_bbox 之前尝试调用 fig.canvas.draw().确切的行为取决于后端,因此在不同平台上会有所不同,但一般来说,您需要先绘制画布,然后再尝试从中复制内容.

Try calling fig.canvas.draw() before calling fig.canvas.copy_from_bbox. The exact behavior depends on the backend, so it will be different on different platforms, but generally speaking you need to draw the canvas before trying to copy things from it.

这篇关于动画 Matplotlib 面板 - blit 留下旧帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆