Funcanimation像time.sleep()一样运行重复函数?

时间:2018-05-30 10:07:17

标签: python python-3.x animation matplotlib

这是我的问题:如果我得到一个带有funcAnimation实现的代码(财务领域,每隔一段时间从交换和实时绘图中获取数据)..如何以优化方式每隔一段时间运行一个函数?

想象一下,我们将此代码作为示例:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x = np.linspace(-6, 6)
tmax = 1
tmin = -1
t = np.linspace(-1, 1)

def f(x, t):
   term = (np.exp(-1*(x-3*t)**2))*np.sin(3*np.pi*(x-t))
   return term

y = f(x, tmax)
lines = plt.plot(x, y)
plt.axis([x[0], x[-1], -1, 1])
plt.xlabel('x')
plt.ylabel('f')

counter = [0]
def animate(ts):
   y = f(x, ts)
   lines[0].set_ydata(y)
   plt.legend(['ts=%4.2f' % ts])
   #plt.savefig('tmp_%04d.png' % counter)
   counter[0] += 1

anim = animation.FuncAnimation(plt.gcf(), animate, frames=t, interval = 1000)
plt.show()

在这种情况下,这可能是如此简单,当我运行它时,代码会被funcAnimation卡在闭环上。

我做什么?把函数放在animate函数里面运行。但是......这是一个很好的方法吗?

这里我们有一部分代码和一个简单的例子:

fig = plt.figure(figsize=(16,18),facecolor="#232a3b")

def animate(i):
    say(5)
graphData('BTC/USDT','1m',8,13,21)

def say(i):
    print('Hello World ' + str(i))

while True:
    #pair = input('Crypto pair to plot: ')
    #timeframe = input('Enter timeframe: ')
    say(5)
    ani = animation.FuncAnimation(fig, animate,interval=60000)
    plt.show()

0 个答案:

没有答案