使用matplotlib.animation从CSV文件进行实时绘图-数据绘制到第一个输入错误 [英] Live plotting from CSV file with matplotlib.animation - Data drawn to first entry error

查看:110
本文介绍了使用matplotlib.animation从CSV文件进行实时绘图-数据绘制到第一个输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制来自传感器的数据,该传感器不断被写入CSV文件。成功创建实时绘图后,每个新数据条目都会创建一条附加行,并延伸到第一个数据条目。参见下文:

I am attempting to plot data from a sensor that is continuously being written to a CSV file. While successful in creating a live plot, each new data entry creates an additional line that extends to the first data entry. See Below:

Python 3.4脚本:

The Python 3.4 Script:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import datetime as dt
import csv



fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
x=[] ; y1=[]; y2=[]; y3=[]
def animate(i):
    with open( 'data_log.csv', 'r') as csvfile:

        alphasensefile = csv.reader(csvfile, delimiter = ',')
        next(alphasensefile, None)
        next(alphasensefile, None)
        next(alphasensefile, None)

        for column in alphasensefile:
            a = dt.datetime.strptime((column[0]), '%H:%M:%S')
            x.append((a))
            y1.append(column[1])
            y2.append(column[2])
            y3.append(column[3])





    ax1.clear()
    ax1.plot(x,y1)
ani = animation.FuncAnimation(fig, animate, interval=1000)

plt.show()

运行此脚本可收集传感器数据并将其记录到CSV文件中。从此开始实时记录的每个数据输入都画了一条额外的线,该线指向第一个数据输入点。像这样:

Running this script collects sensor data and records it to a CSV file. Every data entry recorded live from that start draws an additional line that goes to the first data entry point. Like so:

如果我在未记录传感器的情况下打开文件,则只有最后一个数据条目链接到第一个点,例如:

if I open the file while the sensor is not recording, only the last data entry is linked to the first point like so:

数据记录到像这样的CSV文件:

Data is recorded to the CSV file like so:


PM记录的数据:23 03 2018

PM Data Recorded: 23 03 2018

时间,PM 1,PM 2.5,PM 10

Time, PM 1, PM 2.5, PM 10

16:12:10、0.1173、0.1802、3.2022

16:12:10, 0.1173, 0.1802, 3.2022

是否有人想到这种情况?

Any thoughts to why this is occurring?

推荐答案

FIX:

过一会儿代码,我将代码更改为:

After a while playing around with the code, i changed the code to:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.dates as matdate
from matplotlib import style
import time
import datetime as dt
import csv

style.use('bmh')


fig = plt.figure()
ax = fig.add_subplot(1,1,1)


def animate(i):
    csvfile = open( 'data_log.csv', 'r+')       
    alphasensefile = csv.reader(csvfile, delimiter = ',')
    next(alphasensefile, None)
    next(alphasensefile, None)
    next(alphasensefile, None)

    x=[] ; y1=[]; y2=[]; y3=[]

    for column in alphasensefile:
        a = dt.datetime.strptime((column[0]), '%H:%M:%S')
        x.append((a))
        y1.append(column[1])
        y2.append(column[2])
        y3.append(column[3])

        ax.clear()
        ax.plot(x, y1, label = 'PM 1 ', linewidth=0.7)

ani = animation.FuncAnimation(fig, animate, interval=1000)

plt.show()

数据实时且流畅无问题。

This plots the data live and smooth without issues.

这篇关于使用matplotlib.animation从CSV文件进行实时绘图-数据绘制到第一个输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆