0

Given data of the form

Wed May 27 18:00:02 2020    2029.63
Wed May 27 19:00:04 2020    2029.63
Wed May 27 20:00:06 2020    2029.63
Wed May 27 21:00:04 2020    2029.63
Wed May 27 22:00:03 2020    2029.63
Wed May 27 23:00:03 2020    1663.70
Thu May 28 00:00:05 2020    1663.70
Thu May 28 01:00:04 2020    1663.70
Thu May 28 02:00:03 2020    1663.70
Thu May 28 13:00:04 2020    2029.63
Thu May 28 14:00:06 2020    2029.63
Thu May 28 15:00:04 2020    2029.63
Thu May 28 16:00:02 2020    2029.63
Thu May 28 17:00:06 2020    2029.63
Thu May 28 18:00:03 2020    2029.63
Thu May 28 19:00:09 2020    2029.63

I'm trying to create a plot date/time vs. value. My approach for this was very straight forward (although a bit hacky...)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import datetime

data = open("data.txt").read().split("\n")
data = [x for x in data if x!='']

date = []
values = []

for line in data:
    index = line.find("\t")
    time_str = str(line[4:index])
    date_time_obj = datetime.datetime.strptime(time_str, '%b  %d %H:%M:%S %Y')
    date.append(date_time_obj)
    values.append(line[index+1:])

#dates = mpl.dates.date2num(date)
dates = [x.date() for x in date]

dates_sorted, values_sorted = zip(*sorted(zip(dates, values)))

fig = plt.figure()
plt.plot(dates_sorted, values_sorted, 'ro')
fig.autofmt_xdate()
plt.show() 

This produces a plot, but unfortunately the value-axis is not sorted correctly. To fix this I tried the suggestion in this post, but that also didn't help.

Sito
  • 494
  • 10
  • 29

1 Answers1

1

It might save you some time to have a look at pandas.

import io
import pandas as pd

s = """Timestamp,Value
Wed May 27 18:00:02 2020,2029.63
Wed May 27 19:00:04 2020,2029.63
Wed May 27 20:00:06 2020,2029.63
Wed May 27 21:00:04 2020,2029.63
Wed May 27 22:00:03 2020,2029.63
Wed May 27 23:00:03 2020,1663.70
Thu May 28 00:00:05 2020,1663.70
Thu May 28 01:00:04 2020,1663.70
Thu May 28 02:00:03 2020,1663.70
Thu May 28 13:00:04 2020,2029.63
Thu May 28 14:00:06 2020,2029.63
Thu May 28 15:00:04 2020,2029.63
Thu May 28 16:00:02 2020,2029.63
Thu May 28 17:00:06 2020,2029.63
Thu May 28 18:00:03 2020,2029.63
Thu May 28 19:00:09 2020,2029.63"""

df = pd.read_csv(io.StringIO(s), parse_dates=['Timestamp'])

df.plot(x='Timestamp', y='Value')

Just replace the io.String with the path to the file with your data. Find more info on pd.DataFrame.plot here.

Exemplary output:

enter image description here

FObersteiner
  • 22,500
  • 8
  • 42
  • 72