0

I have been trying to plot CSV stream (been weeks - because of my limited understanding on Python & programming).

Previously, I have asked a question here & thankfully I received the answer, however, I realised that using matplotlib (for loop) generated very slow fps. Here's the current code:

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/r/Desktop/BL/wed10.csv", "r")
logrecords = []
Nmax = 2
fig, ax = plt.subplots()

dataSet = []
tstart= time.time()

for record in follow(logfile):
    logrecords.append(record)
    try:
        values = [float(value) if value else 0.0 for value in record.split(',')]
        values2 = [val2 if val2<1.0 else 0.0 for val2 in values]

    except ValueError:
        continue  # read another record
    ax.plot(values2, color='black', label="%02d'%.2fs"%divmod(time.time()%3600, 60))
    plt.legend()
    if len(ax.lines) == Nmax : ax.lines[0].remove()
    plt.pause(0.00001)

It works, but very slow. I am thinking to use FuncAnimation/ArtistAnimation instead, but I am not sure how to combine it with stream csv. I would like to plot each row (1 plot for each row) & animated to another row.

How can I modify the 'for loop plotting' to FuncAnimation or ArtistAnimation? Thank you.

messymon
  • 33
  • 1
  • 8
  • How large is the csv file? if it's not very large you can forget about `streaming` and try opening the file at x interval and updating a plot. Please look into [this](https://pythonprogramming.net/live-graphs-matplotlib-tutorial/) and [this](https://learn.sparkfun.com/tutorials/graph-sensor-data-with-python-and-matplotlib/update-a-graph-in-real-time). I had done a similiar project couple years ago [github](https://github.com/venky18/WSN/blob/master/plotDistance.py) this reads data from a file which is constantly update and plot. – Equinox Aug 13 '20 at 07:45
  • The csv file will record values from 192 channels (columns) & every 1 sec, there would be 128 rows. the estimation is to get the csv running for 30secs., so the approximate would be 3840x192. I'll try to read the links that you have given me. Thanks ! – messymon Aug 13 '20 at 23:35

0 Answers0