Search code examples
pandascsvtimeseries

Having issues with plotting time series from a CSV file in Python 3


So, I'm trying to plot 2 columns of data (Temperature and Voltage in this case) from a CSV file. But for some weird reason, I'm getting errors about the TimeStamp column, no matter what I try.

The error is: The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "Date_Time_Experiment.py", line 10, in data['TimeStamp'] = pd.to_datetime(data['TimeStamp'], format='%d/%m/%Y-%H:%M:%S') File "/home/pi/.local/lib/python3.7/site-packages/pandas/core/frame.py", line 3458, in getitem indexer = self.columns.get_loc(key) File "/home/pi/.local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc raise KeyError(key) from err KeyError: 'TimeStamp'

The below code is the latest version of what I tried

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from datetime import datetime


plt.style.use('fivethirtyeight')

def animate(i):
    data = pd.read_csv('Logging.csv', skiprows=1)
    data['TimeStamp'] = pd.to_datetime(data['TimeStamp'], format='%d/%m/%Y-%H:%M:%S')
    
    x=data['TimeStamp'] 
    y1=data['Temperature']
    y2=['Voltage']
    plt.cla()
    plt.plot(x,y1,label='Temperature')
    plt.plot(x,y2,label='Voltage')
    
    plt.legend(loc='upper left')
    plt.tight_layout()
    
ani=FuncAnimation(plt.gcf(),animate, interval=61000)

plt.tight_layout()
plt.show()


My CSV file looks like this:

TimeStamp Temperature Voltage
07/03/2023-15:33:45 20.595 10.04591440
07/03/2023-16:15:32 20.630 10.04591070

I've scoured StackOverflow for info and otehr sites, and nothing I try seems to work. Can anyone help me with figuring this out ?

Thanks


Solution

  • I got past the issue by removing skiprows=1. Your data appears in the data frame now.

    The second issue is a typo: you left off data when you assigned the values for Voltage to y2. (Compare the code you posted with mine.)

    There's one last issue with your animate call. I don't know what that's about. When I comment that out I get a plot of temperature and voltage versus time.

    I found a tutorial for FuncAnimation here. I have to do a bit more work to make it function on my Macbook Pro. Maybe it'll help you.

    # see https://stackoverflow.com/questions/75673364/having-issues-with-plotting-time-series-from-a-csv-file-in-python-3
    
    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    from datetime import datetime
    
    plt.style.use('fivethirtyeight')
    
    def animate():
        data = pd.read_csv('../resources/Logging.csv', header=0)
        data['TimeStamp'] = pd.to_datetime(data['TimeStamp'], format='%d/%m/%Y-%H:%M:%S')
    
        x = data['TimeStamp']
        y1 = data['Temperature']
        y2 = data['Voltage']
    
        plt.cla()
        plt.plot(x, y1, label='Temperature')
        plt.plot(x, y2, label='Voltage')
    
        plt.legend(loc='upper left')
        plt.tight_layout()
    
    #    ani = FuncAnimation(plt.gcf(), animate, interval=61000)
    
        plt.tight_layout()
        plt.show()
    
    if __name__ == '__main__':
        animate()