如何在Matplotlib中为文本设置动画? [英] How to animate text in Matplotlib?

查看:297
本文介绍了如何在Matplotlib中为文本设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Matplotlib图形中的文本框设置动画,但似乎无法使其正常工作。有人知道如何正确执行此操作吗?下面是一个示例。

I am trying to animate a text box in a Matplotlib figure, but can't seem to get it working. Does anyone know how to do this properly? An example is below.

from matplotlib import animation
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

fig = plt.figure()
ax = fig.add_subplot(111)

times = ['first', 'second', 'third']

time_text = ax.text(.5, .5, '', fontsize=15)


def updatefig(num):
    global mt
    mt = ax.text(.5, .5, times[num], fontsize=15)

anim = animation.FuncAnimation(fig, updatefig, frames=len(times)-1, blit=True, init_func=init)


推荐答案

文本是艺术家,您可以像其他任何艺术家为其设置动画:

Text is an artist and you animate it exactly like any other artist:

def updatefig(num):
    time_text.set_text(times[num])
    return time_text,

这篇关于如何在Matplotlib中为文本设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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