Matplotlib tkagg后端性能 [英] Matplotlib tkagg backend performance

查看:111
本文介绍了Matplotlib tkagg后端性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tkinter应用程序,可以绘制大量数据,当画布上有大量数据时,我注意到平移和缩放性能很差.

I have a tkinter application that can plot a large number of data, where I have noticed poor pan and zoom performance when there is a large number of data on the canvas.

查看 tkagg_backend(如 this 和其他几个问题的建议),函数和文档建议只有在用户空闲时才应重新绘制画布.但是,根据当前和以前的经验,画布始终在中间缩放/平移中进行更新(重新绘制).因此,我正在查看所涉及的具体功能,并对此有疑问.

Looking into the tkagg_backend (as suggested by this and several other questions), the function and documentation suggests that the canvas should only be redrawn once the user is idle. However, from current and previous experience the canvas has always been updating(redrawing) mid zoom/pan. Therefore, I was looking at the specific functions that are involved and have a question regarding that.

dynamic_update函数:

The dynamic_update function:

def dynamic_update(self):
    'update drawing area only if idle'
    # legacy method; new method is canvas.draw_idle
    self.canvas.draw_idle()

canvas.draw_idle()函数:

The canvas.draw_idle() function:

def draw_idle(self):
    'update drawing area only if idle'
    if self._idle is False:
        return

    self._idle = False

    def idle_draw(*args):
        try:
            self.draw()
        finally:
            self._idle = True

    self._idle_callback = self._tkcanvas.after_idle(idle_draw)

._ idle 参数在后端初始化为 True .这是我被困住的地方,因为我无法理解 ._ idle 与鼠标活动的关联(我想是的,如果那是错误的,请纠正我).

The ._idle parameter is initialized as True in the backend. This point is where I got stuck as I am unable to understand how ._idle is linked to mouse activity (I assume that it is, please correct me if that's wrong).

有趣的是,通过注释 self.canvas.draw_idle() 行(松开鼠标按钮后重绘),画布的行为就像我期望的那样,因此不会调用整个 draw_idle 函数.

Interestingly enough, the canvas behaves like I would expect by commenting the self.canvas.draw_idle() line (redrawing once the mouse button is unpressed), and thus not calling the entire draw_idle function.

因此,我的问题是 _idle 是如何设置的,或者为什么当我不是 idle 时会重新绘制整个画布?

Therefore, my question is how is _idle set or why does it redraw my entire canvas when I am not idle?

推荐答案

所指的是处于空闲状态",不是用户或其鼠标活动,而是GUI主循环.只有当主循环当前不忙时,才应重绘画布.这里,当然 self._idle 仅指 GUI 的 matplotlib 部分,draw_idle 内部的这个结构应该做的是防止画布在绘制时被绘制正在绘制中.

When refering to "being idle" it is not the user or his mouse activity that is meant, but rather the GUI mainloop. The canvas should only be redrawn if the mainloop is not currently busy. Here, of course self._idle only refers to the matplotlib part of the GUI and what this structure inside draw_idle is supposed to do is to prevent the canvas from being drawn while it is being drawn.

平移或缩放时很容易发生这种情况.鼠标移动到新位置,导致重新绘制.当这次重绘发生时,鼠标已经移动得更远并导致下一次重绘.在那个时间点,第一次重画可能还没有完成,以至于它会排队.依此类推,在某些时候 GUI 可能会变得无响应.为了防止这种情况,新的绘制仅在前一个绘制完成后初始化,并且这种行为由 self._idle 是真还是假来控制.

This could easily happen when panning or zooming. The mouse moves to a new location, causing a readraw. While this redraw is happening, the mouse has already moved further and caused the next redraw. At that point in time the first redraw may not yet have finished, such that it would queue up. And so forth, such that at some point the GUI might become unresponsive. To prevent that, a new draw is only initialized once the previous one has finished and this behaviour is steered by self._idle being true or false.

这篇关于Matplotlib tkagg后端性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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