欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实时动态扫描数据流

程序员文章站 2022-03-21 16:28:48
...
import datetime
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
plt.rcParams["font.sans-serif"]=['SimHei']
hour = 10
step = 60
second = int(float(hour)*60*60)
now = datetime.datetime.now()
MeanValue, SumValue, MeanIndex = 0, 0, 0
delta = datetime.timedelta(seconds=60)
xmin = now - delta
ymax = 0
plt.ion()
plt.figure(1)
ax = plt.subplot(111)
Meanline = []
for i in range(0, second, step):
    m = (1500 - 100 + 200 * np.random.random())/380
    if ymax < m:
        ymax = m
    now += delta
    SumValue += m
    xmax = now
    MeanValue = SumValue/(MeanIndex + 1)
    MeanIndex += 1
    if i == 0:
        x = [now - delta, now]
        y = [m-1, m]
        plt.plot(x, y, "-")
    else:
        x.pop(0)
        x.append(now)
        y.pop(0)
        y.append(m)
        plt.plot(x, y, "-")
    if (xmax - xmin) > datetime.timedelta(hours=1):
        xmin = xmax - datetime.timedelta(hours=1)
    plt.xlim(xmin, xmax)
    plt.ylim(0, 1.5*ymax)
    ax1 = plt.axhline(y=MeanValue, color="b", linewidth=1, linestyle="--")
    Meanline.append(ax1)
    if i > 1:
        Meanline[0].remove()
        Meanline.pop(0)

    plt.xticks(rotation=50)
    ax.xaxis.set_major_formatter(mdates.DateFormatter("%m-%d %H:%M:%S"))
    plt.pause(1)
相关标签: pyplot