我是python和matplotlib的新手,需要一些指针。我正在尝试编写一个查询表并绘制结果的监视器。从表中,我可以提取要用于X轴的时间戳和要用于Y值(发送的数据包数量)的#of秒。我不确定动画功能在哪里填充“ i”。我的情节出现了,但是空的。我不确定应将ax.set_xlim设置为什么,最后我如何使日期/时间戳显示在x轴上?
我正在尝试修改以下示例:

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()

最佳答案

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进行测试,它有点慢(这不是问题,因为您要更新2s),但是要正常工作则不太灵活。

也试试

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


在您运行任何东西之前。

关于python - Matplotlib监视器-每X秒从表中绘制一次值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16111529/

10-14 19:28
查看更多