`matplotlib`:艺术家的动画状态的目的是什么? [英] `matplotlib`: what is the purpose of an artist's animated state?

查看:44
本文介绍了`matplotlib`:艺术家的动画状态的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matplotlib 中的艺术家有设置/获取其动画状态(布尔值)的方法.我似乎找不到文档来说明动画状态"变量的用途.您能解释一下,还是指向合适的资源?

Artists in matplotlib have methods to set/get their animated state (a boolean). I can't seem to find documentation to explain the purpose of the "animated state" variable. Can you explain, or point me to an appropriate resource?

推荐答案

我不确定它是否在任何地方都有完整的文档记录,但是艺术家的动画状态控制在绘制绘图时是否包含它.

I'm not sure it's thoroughly documented anywhere, but the animated state of an artist controls whether or not it's included when drawing the plot.

如果animated 为True,则调用fig.draw() 时不会绘制艺术家.相反,它只会在您手动调用 draw_artist(artist_with_animated_set) 时绘制.这允许简化发条功能.

If animated is True, then the artist will not be drawn when fig.draw() is called. Instead, it will only be drawn when you manually call draw_artist(artist_with_animated_set). This allows for simplification of blitting functions.

注意:这不适用于所有后端!我认为它适用于几乎所有交互式后端,但不适用于非交互式后端.它旨在与 blitting 结合使用,因此不支持 blitting 的后端不支持动画标志.

Note: This does not apply to all backends! I think it applies to almost all interactive backends, but it doesn't apply to non-interactive backends. It's intended to be used in combination with blitting, so backends that don't support blitting don't support the animated flag.

例如,如果我们做类似的事情:

For example, if we do something similar to this:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10), animated=True)
plt.show()

我们会得到一个空白图——不会画线.(注意:如果您保存此图,该行显示.请参阅上面关于非交互式后端的警告.Matplotlib 临时切换到非交互式后端以保存图.)

We'll get a blank plot -- the line will not be drawn. (Note: If you save this figure, the line will show up. See the caveat about non-interactive backends above. Matplotlib temporarily switches to a non interactive backend to save a figure.)

为了了解为什么这很有用,假设您正在制作动画或交互式 gui(并且没有使用新的动画框架).您将需要使用blitting使动画显示为平滑".

To get a sense of why this is useful, suppose you were making an animation or interactive gui (and weren't using the new animation framework). You'll want to use blitting to make the animation appear "smooth".

但是,无论何时调整图形大小等,都需要更新动画的背景.处理此问题的最佳方法是将回调连接到draw事件.如果没有 animated 标志,您将不得不在绘制回调重新绘制绘图,这将导致无限循环.(解决方法是断开并重新连接绘制事件回调,但这有点麻烦.)

However, whenever the figure is resized, etc, the animation's background will need to be updated. The best way to handle this is to connect a callback to the draw event. Without the animated flag, you'll have to redraw the plot inside your draw callback, which will cause an infinite loop. (The workaround is to disconnect and reconnect the draw event callback, but that's a bit of a pain.)

无论如何,动画标记大大简化了此过程.例如,您可以在类似以下内容的地方使用 animated 标志:

At any rate, the animated flag simplifies this process quite a bit. For example, you might use the animated flag in something similar to the following:

import numpy as np
import matplotlib.pyplot as plt

class AnimatedSinWave(object):
    def __init__(self, speed=0.1, interval=25):
        self.fig, self.ax = plt.subplots()
        self.x = np.linspace(0, 6 * np.pi, 200)
        self.i, self.speed = 0.0, speed

        self.line, = self.ax.plot(self.x, np.sin(self.x), animated=True)
        self.ax.set_title('Try resizing the figure window')

        self.fig.canvas.mpl_connect('draw_event', self.update_background)
        self.t = self.fig.canvas.new_timer(interval, [(self.update, [], {})])
        self.t.start()

    def update_background(self, event):
        self._background = self.fig.canvas.copy_from_bbox(self.ax.bbox)

    def update(self):
        self.fig.canvas.restore_region(self._background)

        self.line.set_ydata(np.sin(self.i * self.speed + self.x))
        self.ax.draw_artist(self.line)
        self.i += 1

        self.fig.canvas.blit(self.ax.bbox)

    def show(self):
        plt.show()

AnimatedSinWave().show()

注意:由于各种原因,OSX 和 qt*Agg 后端会发生一些奇怪的事情.如果首次弹出窗口时背景为黑色,请移动或聚焦窗口,并且它应该自行修复.

Note: For various reasons, you'll have some odd things happen on the OSX and qt*Agg backends. If the background is black when the window first pops up, move or focus the window, and it should fix itself.

无论如何,如果没有 animated 标志(或更新的动画框架),该示例会变得复杂.

At any rate, without the animated flag (or the newer animation framework), that example becomes much more complex.

这篇关于`matplotlib`:艺术家的动画状态的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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