3

I am trying to make a live plot in matplotlib, meaning that the data is coming from a CSV file that keeps getting updated with new data. So far, I can't succeed to make the plot update continually.

My intention is that I want that with time passing the graph old point will get out of the plot figure. Can someone help me please?

This is my code:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

import csv
import time



fig=plt.figure()
ax1=fig.add_subplot(1,1,1)


def animate(i):
    graph_data=open('DATA.csv')
    xs=[]
    ys=[]
    for line in graph_data:
        time,hrt=line.split(',')
        xs.append(float(time))
        ys.append(float(hrt))
    ax1.clear()
    ax1.plot(xs,ys,'b',linewidth=0.5)




ani=animation.FuncAnimation(fig,animate,interval=8)
plt.show()
mareoraft
  • 3,474
  • 4
  • 26
  • 62
nadavz05
  • 33
  • 4

1 Answers1

0

Use How to update/refresh my graph (animated graph) after another cycle, with matplotlib?
but open the file outside of the function.

Inside of the function check if there is new data. It's not that good to use it from a file.

  • You could count the number of lines that you read and reopen the file new each time and skip that number of lines.

  • You could check the time stamp and read the last line.

  • You could read all the data and plot the whole data again. Ok for little data.

  • You could open the file in binary mode and check the number of bytes you read and then read up to the next line separator.

Joe
  • 6,758
  • 2
  • 26
  • 47