Getting Date/time And Data Out Of Csv Into Matplotlib
My end goal is to get date/time and temp data plotted by matplotlib from a csv logfile, data is formatted as such: date/time,temp1,temp2,temp3,temp4,ect 8/25/2017 14:55:49,20.851,2
Solution 1:
Pandas is good at handling datetime formats, and it's integrated nicely with Matplotlib.
For example, with your example data:
datetime,temp1,temp2,temp3,temp4
8/25/2017 14:55:49,20.851,20.953,21.025,21.055
8/25/2017 14:56:49,20.799,20.944,20.99,21.029
If that's saved as example.csv
, you can do:
import pandas as pddf= pd.read_csv("example.csv", parse_dates=['datetime'], index_col="datetime")
df.plot()
Solution 2:
Once you split the datetime string of your CSV, you can use strptime, as in the following example:
from datetime import datetime
parsed_value = '8/25/2017 14:55:49'
date = datetime.strptime(parsed_value, "%m/%d/%Y %H:%M:%S")
print(type(date))
print(date)
returns:
class'datetime.datetime'>2017-08-2514:55:49
Process finished withexit code 0
Post a Comment for "Getting Date/time And Data Out Of Csv Into Matplotlib"