在 Python 上绘制从 CSV 流获取的字符串值 [英] Plotting string values acquired from CSV stream on Python

查看:41
本文介绍了在 Python 上绘制从 CSV 流获取的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有强大的Python&编程背景,目前我坚持绘制从 csv 流中获取的数据/值.

当前,这就是我只打印从.asv(来自传感器的实时数据)流式传输的值的方法,该值是我从

通过运行上面的脚本,我得到了这样的值行(这里是许多行中的 2 行的示例):

<块引用><预> <代码> 0,10180,0.518193,0.515263,0.517216,0.518193,0.517705,0.517216,0.516972,0.515995,0.514042,0.510867,0.507448,0.503053,0.498657,0.492796,0.486935,0.480586,0.472283,0.464713,0.454701,0.443956,0.431746,0.418071,0.404396,0.391697,0.378022,0.364347,0.352625,0.341880,0.333089,0.324298,0.317216,0.310379,0.302808,0.295238,0.286203,0.277411,0.267643,0.258852,0.249084,0.240293,0.231013,0.223687,0.2168500.2,0.210256,0.199267,0.194383,0.188523,0.184615,0.180220,0.175824,0.171917,0.167521,0.163614,0.160684,0.157265,0.154823,0.153358,0.151893,0.150916,0.150427,0.150427,0.150672,0.152869,0.455433,0.447131,0.443956,0.440537,0.437607,0.434676,0.433700,0.433211,0.433211,0.434188,0.434921,0.436874,0.439072,0.441026,0.442491,0.444444,0.445910,0.446398,0.447375,0.448840,0.449817,0.450794,0.450794,0.451038,0.451770,0.453480,0.455678,0.459096,0.462515,0.468376,0.473260,0.478144,0.482540,0.487180,0.491819,0.496215,0.500122,0.504762,0.507937,0.511600,0.515018,0.517949,0.520147,0.522100,0.524298,0.525519,0.526007,0.526007,0.525031,0.523565,0.520879,0.517705,0.514286,0.510379,0.505983,0.500611,0.496215,0.490354,0.483761,0.478877,0.473260,0.467888,0.463980,0.547497,0.565812,0.579731,0.596337,0.612943,0.643712,0.658364,0.671551,0.683272,0.694994,0.706716,0.716484,0.724786,0.731380,0.736996,0.741392,0.742857,0.743101,0.742857,0.741880,0.739927,0.736020,0.730647,0.724298,0.716484,0.706716,0.694506,0.681807,0.668620,0.653968,0.638828,0.622711,0.606838,0.591453,0.576801,0.560684,0.546032,0.530647,0.516239,0.501587,0.486935,0.474725,0.461783,0.449817,0.438584,0.428327,0.417582,0.408303,0.399512,0.391209,0.382418,0.372650,0.362393,0.351160,0.340171,0.329182,0.317460,0.307204,0.298901,0.291087,0.284005,0.278388,0.2749690,10181,0.519658,0.516239,0.517949,0.518681,0.518193,0.517460,0.516728,0.515751,0.513797,0.510379,0.507448,0.503053,0.497680,0.493285,0.486935,0.480586,0.472772,0.464957,0.454945,0.443956,0.431746,0.418071,0.405128,0.391209,0.378022,0.365324,0.352625,0.342369,0.332601,0.324786,0.316972,0.310134,0.303053,0.294994,0.286691,0.277900,0.268132,0.258852,0.249573,0.240293,0.231502,0.224176,0.217338,0.210989,0.205372,0.19975699,0.1943,0.185592,0.180708,0.176313,0.171673,0.167521,0.163614,0.160195,0.157753,0.155067,0.153358,0.152381,0.150916,0.150916,0.150427,0.150672,0.152869,0.455189,0.446398,0.443468,0.439560,0.437118,0.435653,0.433944,0.432723,,0.433211,0.433700,0.434676,0.436630,0.438828,0.441026,0.442979,0.444444,0.445421,0.446398,0.447375,0.448352,0.449573,0.450794,0.450794,0.451282,0.451770,0.453236,0.455189,0.458364,0.462515,0.468376,0.472772,0.477900,0.482540,0.487424,0.491819,0.496215,0.499634,0.504518,0.507937,0.511355,0.514530,0.517216,0.519658,0,652520.524542,0.525519,0.526007,0.524542,0.522589,0.5203​​91,0.517216,0.514286,0.509890,0.505495,0.500611,0.495727,0.490354,0.484982,0.479121,0.472772,0.467643,0.464469,0.547985,0.566056,0.579731,0.596581,0.613431,0.6290600.6,0.658852,0.672527,0.684249,0.695482,0.706227,0.716972,0.724786,0.731136,0.736996,0.740904,0.742857,0.742857,0.742857,0.742369,0.740415,0.736508,0.730647,0.724298,0.716484,0.706471,0.694506,0.682296,0.668620,0.653968,0.623199,0.606593,0.591453,0.576313,0.560684,0.545543,0.530891,0.516239,0.501587,0.487424,0.474237,0.461538,0.449573,0.438584,0.427839,0.418559,0.408791,0.399512,0.390720,0.381685,0.372894,0.362393,0.351648,0.340415,0.329182,0.318437,0.307692,0.298413,0.291087,0.284249,0.279365,0.275458

我想绘制每一行(每行 1 个图).但是,我意识到这些值表示为'str'.那么,如何将其更改为另一种数据类型,以便我可以使用 Matplotlib 绘制它?

解决方案

我的想法如下

  • 打开文件
  • 准备我们将要使用的对象——注意我们不应该在一个图上绘制太多曲线
  • 读取仪器,记录一次,循环播放
    • 将新记录附加到您将在循环结束时处理的记录列表中,即当生成器 returns
    • 将当前记录转换为浮点数列表
    • 绘制值
    • 检查 Axes 对象中的行列表,如果它太长(就我们的 Nmax 而言),我们将删除最旧的情节中的直线.

就这样:

 导入时间日志文件=打开(...)日志记录 = []Nmax = 10#十只是个数字,你必须看看什么对你有好处无花果,ax = plt.subplots()记录在follow(logfile)中:logrecords.append(记录)尝试:values = [float(tok) if tok else 0.0 for tok in record.split(',')]除了ValueError:继续#阅读另一条记录ax.plot(values, label="%02d'%.2fs"%divmod(time.time()%3600, 60))plt.legend()如果 len(ax.lines) == Nmax : ax.lines[0].remove()暂停(0.01)

请注意,我没有测试上面的'cs 我没有你的数据,如果你发现我的代码有任何问题,我们可以在以后的评论交流中修复它.

I don't have strong Python & programming background, and currently I am stuck on plotting data/values which I acquired from csv stream.

Currently, this is what I have to just print the values streamed from .csv (real-time data from sensor), which I acquired from here:

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/ra/Desktop/mon23.csv","r")
loglines = follow(logfile)

for x in loglines:
     print(x)

From running the script above, I got rows of values like this (here's the example of 2 rows out of many):

0,10180,,0.518193,0.515263,0.517216,0.518193,0.517705,0.517216,0.516972,0.515995,0.514042,0.510867,0.507448,0.503053,0.498657,0.492796,0.486935,0.480586,0.472283,0.464713,0.454701,0.443956,0.431746,0.418071,0.404396,0.391697,0.378022,0.364347,0.352625,0.341880,0.333089,0.324298,0.317216,0.310379,0.302808,0.295238,0.286203,0.277411,0.267643,0.258852,0.249084,0.240293,0.231013,0.223687,0.216850,0.210256,0.204640,0.199267,0.194383,0.188523,0.184615,0.180220,0.175824,0.171917,0.167521,0.163614,0.160684,0.157265,0.154823,0.153358,0.151893,0.150916,0.150427,0.150427,0.150672,0.152869,0.455433,0.447131,0.443956,0.440537,0.437607,0.436142,0.434676,0.433700,0.433211,0.433211,0.434188,0.434921,0.436874,0.439072,0.441026,0.442491,0.444444,0.445910,0.446398,0.447375,0.448840,0.449817,0.450794,0.450794,0.451038,0.451770,0.453480,0.455678,0.459096,0.462515,0.468376,0.473260,0.478144,0.482540,0.487180,0.491819,0.496215,0.500122,0.504762,0.507937,0.511600,0.515018,0.517949,0.520147,0.522100,0.524298,0.525519,0.526007,0.526007,0.525031,0.523565,0.520879,0.517705,0.514286,0.510379,0.505983,0.500611,0.496215,0.490354,0.483761,0.478877,0.473260,0.467888,0.463980,0.547497,0.565812,0.579731,0.596337,0.612943,0.628571,0.643712,0.658364,0.671551,0.683272,0.694994,0.706716,0.716484,0.724786,0.731380,0.736996,0.741392,0.742857,0.743101,0.742857,0.741880,0.739927,0.736020,0.730647,0.724298,0.716484,0.706716,0.694506,0.681807,0.668620,0.653968,0.638828,0.622711,0.606838,0.591453,0.576801,0.560684,0.546032,0.530647,0.516239,0.501587,0.486935,0.474725,0.461783,0.449817,0.438584,0.428327,0.417582,0.408303,0.399512,0.391209,0.382418,0.372650,0.362393,0.351160,0.340171,0.329182,0.317460,0.307204,0.298901,0.291087,0.284005,0.278388,0.274969
0,10181,,0.519658,0.516239,0.517949,0.518681,0.518193,0.517460,0.516728,0.515751,0.513797,0.510379,0.507448,0.503053,0.497680,0.493285,0.486935,0.480586,0.472772,0.464957,0.454945,0.443956,0.431746,0.418071,0.405128,0.391209,0.378022,0.365324,0.352625,0.342369,0.332601,0.324786,0.316972,0.310134,0.303053,0.294994,0.286691,0.277900,0.268132,0.258852,0.249573,0.240293,0.231502,0.224176,0.217338,0.210989,0.205372,0.199756,0.194383,0.189499,0.185592,0.180708,0.176313,0.171673,0.167521,0.163614,0.160195,0.157753,0.155067,0.153358,0.152381,0.150916,0.150916,0.150427,0.150672,0.152869,0.455189,0.446398,0.443468,0.439560,0.437118,0.435653,0.433944,0.432723,0.432723,0.433211,0.433700,0.434676,0.436630,0.438828,0.441026,0.442979,0.444444,0.445421,0.446398,0.447375,0.448352,0.449573,0.450794,0.450794,0.451282,0.451770,0.453236,0.455189,0.458364,0.462515,0.468376,0.472772,0.477900,0.482540,0.487424,0.491819,0.496215,0.499634,0.504518,0.507937,0.511355,0.514530,0.517216,0.519658,0.522100,0.523565,0.524542,0.525519,0.526007,0.524542,0.522589,0.520391,0.517216,0.514286,0.509890,0.505495,0.500611,0.495727,0.490354,0.484982,0.479121,0.472772,0.467643,0.464469,0.547985,0.566056,0.579731,0.596581,0.613431,0.629060,0.644200,0.658852,0.672527,0.684249,0.695482,0.706227,0.716972,0.724786,0.731136,0.736996,0.740904,0.742857,0.742857,0.742857,0.742369,0.740415,0.736508,0.730647,0.724298,0.716484,0.706471,0.694506,0.682296,0.668620,0.653968,0.638584,0.623199,0.606593,0.591453,0.576313,0.560684,0.545543,0.530891,0.516239,0.501587,0.487424,0.474237,0.461538,0.449573,0.438584,0.427839,0.418559,0.408791,0.399512,0.390720,0.381685,0.372894,0.362393,0.351648,0.340415,0.329182,0.318437,0.307692,0.298413,0.291087,0.284249,0.279365,0.275458

I would like to plot each row (1 plot for each row). However, I realised that the values are indicated as 'str'. Then, how do I change it to another data type so I could plot it using Matplotlib?

解决方案

My idea is as follows

  • open the file
  • prepare the objects we will use — note that we shouldn't plot too many curves on a single plot
  • read the instrument, one record a time, in a loop
    • append the new record to a list of records that you will deal with at the end of the loop, i.e., when the generator returns
    • convert the current record to a list of floats
    • plot the values
    • check the list of lines in the Axes object, if it is too long (in terms of our Nmax) we remove the oldest line from the plot.

That's it:

import time

logfile = open(...)

logrecords = []
Nmax = 10 # ten is just a number, you have to see what is good for you
fig, ax = plt.subplots()

for record in follow(logfile):
    logrecords.append(record)
    try:
        values = [float(tok) if tok else 0.0 for tok in record.split(',')]
    except ValueError:
        continue # read another record
    ax.plot(values, label="%02d'%.2fs"%divmod(time.time()%3600, 60))
    plt.legend()
    if len(ax.lines) == Nmax : ax.lines[0].remove()
    plt.pause(0.01)

Note that I have not tested the above 'cs I have not your data, if you find any problem with my code we can fix it in a later exchange of comments.

这篇关于在 Python 上绘制从 CSV 流获取的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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