Home > OS >  How to plot data from csv for specific date and time?
How to plot data from csv for specific date and time?

Time:03-09

I am trying to plot data from a csv-file where the y-axis represents temperature and the x-axis time. But I dont want to plot all of the data, only the data that start at 21/02/2022 10:44:36 and end at 21/02/2022 10:45:36. Here's a sample of the data:

timestamp           resistance 
21/02/2022 10:44:00 10460.5248 
21/02/2022 10:44:01 10460.5048 
21/02/2022 10:44:03 10460.488 
21/02/2022 10:44:04 10460.4776 
21/02/2022 10:44:05 10460.4512 
21/02/2022 10:44:06 10460.4288 
21/02/2022 10:44:07 10460.4

My code below doesn't show the correct values for the x-axis, it seems like the time values aren't read correctly from the file:

beta=3828
K=0.0263

dt_format='%Y-%m-%d %H:%M:%S.%f'

font = {'family': 'serif',
 'color': 'black',
 'weight': 'normal',
 'size': 10,
 }

plt.style.use('fivethirtyeight')

data = pd.read_csv('data_keithley1.csv')

x = data['timestamp']
x_1=x['21/02/2022  10:44:36':'21/02/2022  10:45:36']

R_theta= data['resistance']
T=(beta/np.log(R_theta/K))-273.15

plt.cla()
plt.plot(x_1, T,label='Temperatur [deg C]')
plt.gcf().autofmt_xdate()
plt.legend(loc='upper left')
plt.xlabel('time [hh:mm:ss]', fontdict=font)
plt.ylabel('Temp [deg C]', fontdict=font)
plt.tight_layout()    

plt.show()

I changed the dataset so it looks like this now:

    timestamp               resistance
2022-02-21 10:44:35.792893  10460.5248
2022-02-21 10:44:35.822877  10460.5048
2022-02-21 10:44:35.842826  10460.48
2022-02-21 10:44:35.872597  10460.4776
2022-02-21 10:44:35.892827  10460.4512
2022-02-21 10:44:35.922595  10460.4288

Here is the new code and i think it will work but i keep getting (TypeError: strptime() argument 1 must be str, not float), and i dont know how to fix it.

import pandas as pd
import matplotlib.pyplot as plt
import csv
import numpy as np
from datetime import datetime

dt_format='%Y-%m-%d %H:%M:%S.%f'


data=pd.read_csv('data_keithley1.csv')
start = '2022-02-21 10:45:00.462227'
end = '2022-02-21 10:56:00.004844'

timestamps =list(map(lambda timestamps: datetime.strptime(timestamps, dt_format) , data['timestamp']))
values = list(data['resistance'])


start_index = timestamps.index(start)
end_index = timestamps.index(end)

sub_timestamps = timestamps[start_index : end_index]
sub_values = values[start_index : end_index]

plt.plot(sub_timestamps, sub_values, label='Motstand [ohm]')
plt.xlabel('time [hh:mm:ss]')
plt.ylabel('Mot [ohm]')
plt.subplots_adjust(bottom=0.35)
_, labels = plt.xticks()
plt.setp(labels, rotation=45)
plt.show()

CodePudding user response:

when i run the code i keep getting a plot that looks like this, any suggestions on how i can get it to be a normal plott

Code output

CodePudding user response:

You've almost got it. You have sliced the timestamps to your desired range:

x_1=x['21/02/2022  10:44:36':'21/02/2022  10:45:36']

but did not sliced the resistance values. Here is an example of how to do that, assuming the following dummy dataset:

test.csv:
             timestamp  resistance
0  21/02/2022 10:44:00           0
1  21/02/2022 10:45:00           1
2  21/02/2022 10:46:00           2
3  21/02/2022 10:47:00           3
4  21/02/2022 10:48:00           4
5  21/02/2022 10:49:00           5
6  21/02/2022 10:50:00           6
7  21/02/2022 10:51:00           7

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('test.csv')
start = '21/02/2022 10:44:00'
end = '21/02/2022 10:49:00'

timestamps = list(data['timestamp'])
values = list(data['resistance'])

start_index = timestamps.index(start)
end_index = timestamps.index(end)

sub_timestamps = timestamps[start_index : end_index]
sub_values = values[start_index : end_index]

plt.plot(sub_timestamps, sub_values, label='Temperature [deg C]')
plt.xlabel('time [hh:mm:ss]')
plt.ylabel('Temp [deg C]')
plt.subplots_adjust(bottom=0.35)
_, labels = plt.xticks()
plt.setp(labels, rotation=45)
plt.show()

enter image description here

Also, please note you've parsed your timestamps with dt_format='%Y-%m-%d %H:%M:%S.%f' although your dates are in the format of %d/%m/%Y %H:%M:%S.%f.

  • Related