Skip to content Skip to sidebar Skip to footer

Plotting String Values Acquired From CSV Stream On Python

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 p

Solution 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 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.


Solution 2:

Is this you want?

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

Post a Comment for "Plotting String Values Acquired From CSV Stream On Python"