Matplotlib动画与Blit-如何更新情节标题? [英] Matplotlib animation with blit -- how to update plot title?

查看:184
本文介绍了Matplotlib动画与Blit-如何更新情节标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用matplotlib通过复制背景并加斑点来使图动画化:

I use matplotlib to animate a plot, by copying the background and blitting:

f = Figure(tight_layout=True)
canvas = FigureCanvasTkAgg(f, master=pframe)
canvas.get_tk_widget().pack()
ax = f.add_subplot(111)
# Set inial plot title
title = ax.set_title("First title")
canvas.show()
# Capture the background of the figure
background = canvas.copy_from_bbox(ax.bbox)
line, = ax.plot(x, y)
canvas._tkcanvas.pack()

我会定期更新剧情:

# How to update the title here?
line.set_ydata(new_data)
ax.draw_artist(line)
canvas.blit(ax.bbox)

每次更新地块时,如何更新-尽可能高效地更新地块标题?

How could I update -- as efficiently as possible, the plot title every time I update the plot?

编辑:

title.set_text("New title")
ax.draw_artist(title)

之前或之后

canvas.blit(ax.bbox)

不会更新标题。我认为我应该以某种方式重绘标题艺术家,或者只捕获图形,作为 blit(ax.bbox)将覆盖包括标题在内的整个标题图区域。

does not update the title. I think somehow I should redraw the title artist, or I should only capture the graph, as blit(ax.bbox) overwrites the entire title plot area including the title.

推荐答案

几年后,我大声疾呼地说我也被这个问题困扰在过去的几天里,禁用blit对我来说不是一种选择(因为fps太慢了)。至少对于matplotlib 3.1.3,如果将调整大小事件发送到画布,它将正确刷新背景并使用任何更新重新生成背景。因此,可以通过检测何时需要更新标题来解决此问题,然后调用 fig.canvas.resize_event()强制刷新。希望这对将来的人有帮助!

Swooping in years later to say that I too was stuck on this problem for the last few days, and disabling blit wasn't an option for me (as the fps would become way too slow). For matplotlib 3.1.3 at least, if you send a resize event to the canvas, it properly flushes the background and regenerates it with any updates. So you can work around this by detecting when you need to update the title, and then calling fig.canvas.resize_event() to force a flush. Hope this helps future people!

这篇关于Matplotlib动画与Blit-如何更新情节标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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