绘制从 Python 上的 CSV 流获取的字符串值

Plotting string values acquired from CSV stream on Python

我没有很强的 Python 和编程背景,目前我一直在绘制从 csv 流中获得的 data/values。

目前,这就是我必须打印从 .csv 流式传输的值(来自传感器的实时数据),这是我从 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)

从上面的 运行 脚本中,我得到了这样的值行(这里是许多行中的 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.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

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

这是你想要的吗?

for x in loglines:
   for i in x.split(','):
       print(float(i)) if x != '' else print(0.0)

我的思路如下

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

就是这样:

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)

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