0

This Python program plots lines dynamically from data in a csv file. When the program first starts it dynamically draws points that already exist in the file. This part works as expected. I'd like for any new points added to the file to be subsequently drawn. The problem is that i continues to increment so by the time a new item is added to my csv file the value of i is usually much higher than the index from the csv so it never gets plotted. How can I prevent the count of i continuing on until there is an applicable value in the csv file?

import numpy as np
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.style.use('fivethirtyeight')

data = pd.read_csv('csv_data.csv')
x_vals = []
y_vals1 = []
y_vals2 = []

index = count()


def animate(i):
    x = data['x_value']
    y1 = data['total_1']
    y2 = data['total_2']
    x_vals.append(x[i])
    y_vals1.append(y1[i])
    y_vals2.append(y2[i])

    plt.cla()
    plt.plot(x_vals, y_vals1, label='Channel 1')
    plt.plot(x_vals, y_vals2, label='Channel 2')

    plt.legend(loc='upper left')

    plt.tight_layout()


ani = FuncAnimation(plt.gcf(), animate, interval=100)

plt.show()
runnerpaul
  • 5,942
  • 8
  • 49
  • 118

0 Answers0