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)
实时动态扫描数据流
最新推荐文章于 2024-03-14 21:41:20 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)