0

我一直在尝试绘制 CSV 流(已经几周了——因为我对 Python 和编程的理解有限)。

以前,我在这里问过一个问题,幸好我收到了答案,但是,我意识到使用 matplotlib(for 循环)会产生非常慢的 fps。这是当前代码:

def follow(thefile):
    thefile.seek(0,2) #file handling on the data stream
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

logfile = open("C:/Users/r/Desktop/BL/wed10.csv", "r")
logrecords = []
Nmax = 2
fig, ax = plt.subplots()

dataSet = []
tstart= time.time()

for record in follow(logfile):
    logrecords.append(record)
    try:
        values = [float(value) if value else 0.0 for value in record.split(',')]
        values2 = [val2 if val2<1.0 else 0.0 for val2 in values]

    except ValueError:
        continue  # read another record
    ax.plot(values2, color='black', label="%02d'%.2fs"%divmod(time.time()%3600, 60))
    plt.legend()
    if len(ax.lines) == Nmax : ax.lines[0].remove()
    plt.pause(0.00001)

它有效,但非常慢。我正在考虑FuncAnimation/ArtistAnimation改用,但我不确定如何将它与流 csv 结合使用。我想绘制每一行(每行 1 个绘图)并动画到另一行。

如何将“for loop plotting”修改为 FuncAnimation 或 ArtistAnimation?谢谢你。

4

0 回答 0