1

I'm new to python and matplotlib and need a few pointers. I'm trying to write a monitor that queries a table and plots the results. from the table I can extract a timestamp I'd like to use for the X axis and a #of seconds I'd like to use for the Y value (number of packets sent). I'm not sure where "i" is populated for the animate function. My plot comes up but is empty. I'm not sure what ax.set_xlim should be set to and finally how do I get the date/timestamp to show up on the x-axis? Im trying to modify the following example:

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

fig = plt.figure()
ax = plt.axes(ylim=(0, 45))
line, = ax.plot([], [], lw=5)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x,y,dk=getData()
    line.set_data(x, y)
    return line,

def Execute():
    #anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=200, blit=True)
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=2000)
    plt.show()
    return(anim)

def getDataSql(sql):
    ... run sql
    return(rl)

def getData():
    ...format return for getDataSql 
    ...return X ex(2013-04-12 18:18:24) and Y ex(7.357) (both are lists)
    return(X,Y,xy)

x=Execute()
4

1 回答 1

1
def Execute():
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=2000, blit=True)
    plt.show() 
    return anim

anim = Execute()

如果您不返回其中的anim对象(其中包含所有计时器等),它会在Execute返回时被垃圾收集,这会删除所有这些对象,因此您的动画不会运行。

您也可以使用 进行测试blit=False,它有点慢(这不是问题,因为您正在更新每 2 秒),但要正常工作有点不那么麻烦。

也试试

ax.get_xaxis().set_major_locator(matplotlib.dates.AutoDateLocator())
ax.get_xaxis().set_major_formatter(matplotlib.dates.AutoDateFormatter())

在你运行任何东西之前。

于 2013-04-19T19:39:14.630 回答