问题
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?
回答1:
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
return
s - 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 ourNmax
) we remove the oldest line from the plot.
- 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
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.
回答2:
Is this you want?
for x in loglines:
for i in x.split(','):
print(float(i)) if x != '' else print(0.0)
来源:https://stackoverflow.com/questions/63333801/plotting-string-values-acquired-from-csv-stream-on-python